python 正则如何多次匹配?

例如abc1abc2abc3abc
re.findall('abc\w*?abc','abc1abc2abc3abc',re.I)

            结果为['abc1abc', 'abc3abc']
            怎么把abc2abc也提取出来?

5个回答

我google了下

stackoverflow上有人问过这个问题

https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches
https://stackoverflow.com/questions/41045008/return-all-overlapping-patterns-using-re-findall-in-python

网站可能需要科学下才能上,我复制了关键的一段

 import re 
s = "123456789123456789"
matches = re.finditer(r'(?=(\d{10}))',s)
results = [int(match.group(1)) for match in matches]
# results: 
# [1234567891,
#  2345678912,
#  3456789123,
#  4567891234,
#  5678912345,
#  6789123456,
#  7891234567,
#  8912345678,
#  9123456789]

gro=re.findall('(?=(abc\w*?abc))','abc1abc2abc3abc',re.I) 如果你只匹配数字的话请把\w换成\d

不太懂python 正则表达式,但是在JavaScript中这个是无法使用正则实现的.
so 考虑一下自己实现一个字符串方法实现吧

re.findall('(abc.abc)','abc1abc2abc3abc',re.I)

 import re

def search_all(pattern,string, flags=0):
    res=[]
    while len(string)>0:
        sobj=re.search(pattern, string, flags)
        if not sobj:
            break
        span=sobj.span()
        res.append(string[span[0]:span[1]])
        string=string[span[0]+1:]
    return res

print(search_all('abc\w*?abc','abc1abc2abc3abc',re.I))
#输出['abc1abc', 'abc2abc', 'abc3abc']
立即提问
相关内容推荐