Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要Goldilocks regex模式-不要太贪婪,也不要太自私_Python_Regex - Fatal编程技术网

Python 需要Goldilocks regex模式-不要太贪婪,也不要太自私

Python 需要Goldilocks regex模式-不要太贪婪,也不要太自私,python,regex,Python,Regex,我有一组字符串可能看起来像这样: lines_ = ["04/04 1,000.00 Some word132:11bdkljas 14235262634235", "04/04 500.00 A simpler phrase 19058453049854", "04/04 1,000,000.00 Apply//erklj//1324:123"] r"(\d{2}/\d{2})\s

我有一组字符串可能看起来像这样:

lines_ = ["04/04      1,000.00    Some word132:11bdkljas     14235262634235",
          "04/04        500.00    A simpler phrase           19058453049854",
          "04/04  1,000,000.00    Apply//erklj//1324:123"]
r"(\d{2}/\d{2})\s+([\d,]+\.\d\d)\s+(.+?)(\s+\d+)?"
我正在尝试编写一个正则表达式,它将提取字符串的前三个“元素”。我意识到基于这个例子,我可以简单地使用
re.split(“\s{2,}”)
,然后只获取前三个元素,但我不能保证输入总是有两个或更多的空格分隔我想要的部分。所以我宁愿有一个更健壮的正则表达式来抓取它

我试着用这个:

r"(\d{2}/\d{2})\s+([\d,]+\.\d\d)\s+(.+)(\s+\d+)"
它适用于前两个元素,但不适用于第三个元素,因为没有数字集。因此,我将其调整为:

r"(\d{2}/\d{2})\s+([\d,]+\.\d\d)\s+(.+)(\s+\d+)?"
这适用于第三个元素,但对于前两个元素,它包括第四个元素作为第三个元素的一部分。然后我进一步调整它,使其看起来像这样:

lines_ = ["04/04      1,000.00    Some word132:11bdkljas     14235262634235",
          "04/04        500.00    A simpler phrase           19058453049854",
          "04/04  1,000,000.00    Apply//erklj//1324:123"]
r"(\d{2}/\d{2})\s+([\d,]+\.\d\d)\s+(.+?)(\s+\d+)?"
认为
(.+)
内部的
会使它不那么贪婪,不会吞噬最后一个元素。相反,它给了我第三个元素中第一个单词的第一个字母,就是它

我希望最终得到如下输出:

groups_ = [("04/04", "1,000.00", "Some word132:11bdkljas"),
           ("04/04", "500.00", "A simpler phrase"),
           ("04/04", "1,000,000.00", "Apply//erklj//1324:123")]

任何关于我在正则表达式中缺少什么的建议都将不胜感激。

将此模式与mg选项结合使用

(\d{2}/\d{2})\s+([\d,]+\.\d\d)\s+(.+?)(?:\s+(\d+)|,|$)  

这对您有什么好处

import re
lines_ = ["04/04      1,000.00    Some word132:11bdkljas     14235262634235",
          "04/04        500.00    A simpler phrase           19058453049854",
          "04/04  1,000,000.00    Apply//erklj//1324:123"]

for i, line in enumerate(lines_):
    match = re.search(r'''
        (\d\d/\d\d)    # eg: 04/04
        \s+            # one or more spaces
        ([0-9.,]+)     # digits, with commas and decimal points
        \s+            # one or more spaces
        (.*?)          # "everything else" (non-greedy)
        (\s+\d+)?      # ... optionally followed by one or more 
                       # spaces and a bunch of digits
        $              # end of line
    ''', line, re.VERBOSE)

    if match:
        print i, match.groups()
    else:
        print i, None
当我运行上述命令时,我得到以下输出:

0 ('04/04', '1,000.00', 'Some word132:11bdkljas', '     14235262634235')
1 ('04/04', '500.00', 'A simpler phrase', '           19058453049854')
2 ('04/04', '1,000,000.00', 'Apply//erklj//1324:123', None)

有趣。当我尝试这种方法时(尽管对于带有逗号和小数的数字略有不同),它甚至找不到第三个元素。