Python:重叠正则表达式搜索

-1 python regex

因此,如果我在 python (3.7) 中创建一个如下所示的程序:

import re
regx = re.compile("test")
print(regx.findall("testest"))
Run Code Online (Sandbox Code Playgroud)

并运行它,然后我会得到:

["test"]
Run Code Online (Sandbox Code Playgroud)

即使有两个“测试”实例,它也只向我展示了一个,我认为这是因为第二个“测试”中使用了第一个“测试”中的字母。我怎样才能制作一个程序来["test", "test"]代替我?

Spe*_*rek 5

您将需要使用具有前瞻性的捕获组(?=(regex_here))

import re
regx = re.compile("(?=(test))")
print(regx.findall("testest"))

>>> ['test', 'test']
Run Code Online (Sandbox Code Playgroud)