python 正则表达式 - findall 未按预期返回输出
python regex - findall not returning output as expected
我无法理解 findall,它说...
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.
为什么这个基本的 IP 正则表达式不能像预期的那样与 findall 一起工作?匹配项不重叠,正则表达式确认模式在 re_str.
中突出显示
预期:['1.2.2.3', '123.345.34.3']
实际值:['2.', '34.']
re_str = r'(\d{1,3}\.){3}\d{1,3}'
line = 'blahblah -- 1.2.2.3 blah 123.345.34.3'
matches = re.findall(re_str, line)
print(matches) # ['2.', '34.']
这是因为捕获组 return 如果它们重复,则只捕获最后一个匹配项。
相反,您应该将重复组设置为非捕获,并在外层使用非重复捕获:
re_str = r'((?:\d{1,3}\.){3}\d{1,3})'
请注意,对于 findall
,如果没有捕获组,则会自动选择整个匹配项(如 [=13=]
),因此您可以删除外部捕获:
re_str = r'(?:\d{1,3}\.){3}\d{1,3}'
当您在正则表达式中使用括号时,re.findall()
将仅 return 括号内的组,而不是整个匹配的字符串。在(
后面加一个?:
,告诉它不要用括号来提取组,那么结果应该是整个匹配的字符串。
我无法理解 findall,它说...
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.
为什么这个基本的 IP 正则表达式不能像预期的那样与 findall 一起工作?匹配项不重叠,正则表达式确认模式在 re_str.
中突出显示预期:['1.2.2.3', '123.345.34.3']
实际值:['2.', '34.']
re_str = r'(\d{1,3}\.){3}\d{1,3}'
line = 'blahblah -- 1.2.2.3 blah 123.345.34.3'
matches = re.findall(re_str, line)
print(matches) # ['2.', '34.']
这是因为捕获组 return 如果它们重复,则只捕获最后一个匹配项。
相反,您应该将重复组设置为非捕获,并在外层使用非重复捕获:
re_str = r'((?:\d{1,3}\.){3}\d{1,3})'
请注意,对于 findall
,如果没有捕获组,则会自动选择整个匹配项(如 [=13=]
),因此您可以删除外部捕获:
re_str = r'(?:\d{1,3}\.){3}\d{1,3}'
当您在正则表达式中使用括号时,re.findall()
将仅 return 括号内的组,而不是整个匹配的字符串。在(
后面加一个?:
,告诉它不要用括号来提取组,那么结果应该是整个匹配的字符串。