【问题标题】:Get values using regex in Python在 Python 中使用正则表达式获取值
【发布时间】:2018-07-12 05:39:33
【问题描述】:

如何仅使用正则表达式获取 test1 的值 4 和 5。我有这个字符串:

  test1 =                 -2,                 2,                 2,                 0,                 0,                 0,                 0,                 0,                 0,                 0
  test2=              27000,             20000,             30000,             16200,              8000,              8000,              8000,              8000,              8000,              8000
  test3 =               1500,              1500,              1500,              1500,              1500,              1500,              1500,              1500,              1500,              1500
  test4 =               7000,              7000,              7000,              8000,              8000,              4000,              4000,              4000,              4000,              4000
  test5 =                500

我有这个命令:

`\s(test)\s*=(?P<test>\D.*)`

它选择 2 个组,但我想要值 4 和 5。或者还有其他方法吗?我不想使用熊猫。

【问题讨论】:

    标签: python regex regex-group


    【解决方案1】:
    import re
    
    regex = r"test1\s*=\s*-?\d+,\s*-?\d+,\s*-?\d+,\s*(-?\d+),\s*(-?\d+)"
    
    test_str = YOUR_STR
    
    matches = re.finditer(regex, test_str, re.MULTILINE)
    
    for matchNum, match in enumerate(matches):
        matchNum = matchNum + 1
    
        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
        for groupNum in range(0, len(match.groups())):
            groupNum = groupNum + 1
    
            print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
    

    如果您正在尝试正则表达式,regex101.com 是一个有用的工具。该网站还会为您生成代码。

    我从那里得到了上面的代码。 https://regex101.com/r/WMUbLf/1

    【讨论】:

      【解决方案2】:
      s="""test1 =                 -2,                 2,                 2,                 0,                 0,                 0,                 0,                 0,                 0,                 0
        test2=              27000,             20000,             30000,             16200,              8000,              8000,              8000,              8000,              8000,              8000
        test3 =               1500,              1500,              1500,              1500,              1500,              1500,              1500,              1500,              1500,              1500
        test4 =               7000,              7000,              7000,              8000,              8000,              4000,              4000,              4000,              4000,              4000
        test5 =                500"""
      
      import re
      
      lines = s.splitlines()
      g1 = re.findall('[-0-9]+', lines[3])[1:] # test4
      g2 = re.findall('[-0-9]+', lines[4])[1:] # test5
      print(g1)
      print(g2)
      

      输出:

      ['7000', '7000', '7000', '8000', '8000', '4000', '4000', '4000', '4000', '4000']
      ['500']
      

      【讨论】:

      • 嗨@Andrej,感谢您的回复。我已经试过了。问题是我有超过 10k 个文件。如果我使用 findall 功能。编译需要一些时间。
      • @PUser 我不明白,你有很多短字符串?您可以编译一次正则表达式,然后使用编译后的版本。
      • 我刚刚更新了文件。我只需要具有 4 和 5 个值的 test1。 S抱歉,如果它令人困惑
      猜你喜欢
      • 1970-01-01
      • 2021-07-30
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多