python 正则表达式匹配连续相同字符的问题
a='aa'
b='aaa'
c=re.findall(a,b)
print(c)# 输出结果为['aa'],只有一处匹配位置
但是我觉得应该有两处匹配,输出结果应为['aa','aa'] 怎样解决这个问题啊?
回答:
正则好像不太好弄。可以用代码实现。
其实这个跟leetcode这个题比较像:https://leetcode-cn.com/probl...
简单点就这么写。
In [3]: a = 'aa'In [4]: b = 'aaa'
In [5]: res_list = []
...: for i in range(0, len(b)-len(a)+1):
...: if b[i:i+len(a)] == a:
...: res_list.append(a)
...: print(res_list)
['aa', 'aa']
回答:
文档里面写了的,如下
?re.findall(pattern, string, flags=0)
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.
是非重叠匹配
以上是 python 正则表达式匹配连续相同字符的问题 的全部内容, 来源链接: utcz.com/a/157442.html