如何在Python中获取字符位置列表?

Pie*_*rre 1 python regex

我正在尝试编写一个函数来清理Web应用程序中的unicode输入,我正在尝试重现本页末尾的PHP函数:http://www.iamcal.com/understanding-bidirectional-text /

我在python中寻找相当于PHP的preg_match_all.RE函数findall返回没有位置的匹配,而search只返回第一个匹配.是否有任何函数可以返回每个匹配项,以及文本中的相关位置?

有了字符串abcdefa和模式a|c,我想得到类似的东西[('a',0),('c',2),('a',6)]

谢谢 :)

sam*_*ias 14

尝试:

text = 'abcdefa'
pattern = re.compile('a|c')
[(m.group(), m.start()) for m in pattern.finditer(text)]
Run Code Online (Sandbox Code Playgroud)