如何在字符串中找到子字符串的所有实例?
例如,我有字符串 ( "%1 is going to the %2 with %3"
)。我需要提取此字符串中的所有占位符 ( %1
, %2
, %3
)
当前代码只能找到前两个,因为结尾不是空格。
import re
string = "%1 is going to the %2 with %3"
r = re.compile('%(.*?) ')
m = r.finditer(string)
for y in m:
print (y.group())