假设我有一个像这样的字符串 123456789
。有没有办法从给定的字符串中找到所有可能的现有模式,如下所示
1234
、 2345
、 3456
、 4567
、 5678
、 6789
使用 regex
我这样做了,但找不到解决方案:
import re
regex = r"[1-9]{4}"
test_string = "123456789"
print(re.findall(regex, test_str))
此输出:
['1234', '5678']
任何帮助对我来说都意义重大,谢谢
最佳答案
您可以尝试这个正则表达式:
\d(?=(\d{3}))
示例源(Run here)
import re
regex = r"\d(?=(\d{3}))"
test_str = "123456789"
matches = re.finditer(regex, test_str)
for match in matches:
print(match.group()+match.group(1));
关于python - 如何在正则表达式中找到重叠模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44924223/