Python re.findall打印所有模式

时间:2020-12-24 22:33:03
>>> match = re.findall('a.*?a', 'a 1 a 2 a 3 a 4 a')
>>> match
['a 1 a', 'a 3 a']

How do I get it to print

我如何打印它

['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

Thank you!

谢谢!

3 个解决方案

#1


6  

I think using a positive lookahead assertion should do the trick:

我认为使用积极的先行断言应该可以解决这个问题:

>>> re.findall('(?=(a.*?a))', 'a 1 a 2 a 3 a 4 a')
['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

re.findall returns all the groups in the regex - including those in look-aheads. This works because the look-ahead assertion doesn't consume any of the string.

re.findall返回正则表达式中的所有组 - 包括那些在前瞻中的组。这是有效的,因为前瞻断言不会消耗任何字符串。

#2


5  

You may use alternative regex module which allows overlapping matches:

您可以使用允许重叠匹配的替代正则表达式模块:

>>> regex.findall('a.*?a', 'a 1 a 2 a 3 a 4 a', overlapped = True)
['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

#3


4  

r = re.compile('a.*?a') # as we use it multiple times
matches = [r.match(s[i:]) for i in range(len(s))] # all matches, if found or not
matches = [m.group(0) for m in matches if m] # matching string if match is not None
print matches

gives

['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

I don't know if it is the best solution, but here I test every substring reaching to the end of the string for matching with the given pattern.

我不知道它是否是最好的解决方案,但在这里我测试每个到达字符串末尾的子字符串以匹配给定的模式。

#1


6  

I think using a positive lookahead assertion should do the trick:

我认为使用积极的先行断言应该可以解决这个问题:

>>> re.findall('(?=(a.*?a))', 'a 1 a 2 a 3 a 4 a')
['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

re.findall returns all the groups in the regex - including those in look-aheads. This works because the look-ahead assertion doesn't consume any of the string.

re.findall返回正则表达式中的所有组 - 包括那些在前瞻中的组。这是有效的,因为前瞻断言不会消耗任何字符串。

#2


5  

You may use alternative regex module which allows overlapping matches:

您可以使用允许重叠匹配的替代正则表达式模块:

>>> regex.findall('a.*?a', 'a 1 a 2 a 3 a 4 a', overlapped = True)
['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

#3


4  

r = re.compile('a.*?a') # as we use it multiple times
matches = [r.match(s[i:]) for i in range(len(s))] # all matches, if found or not
matches = [m.group(0) for m in matches if m] # matching string if match is not None
print matches

gives

['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

I don't know if it is the best solution, but here I test every substring reaching to the end of the string for matching with the given pattern.

我不知道它是否是最好的解决方案,但在这里我测试每个到达字符串末尾的子字符串以匹配给定的模式。