各位,
我必须匹配以下模式:
第一个字母必须是N 第二个除 P 以外的任何字母 第三个必须是 S 或 T 和第四个除 P 之外的任何字母。
字符串只有大写字母,没有数字,空格等
所以使用 python 这是我到目前为止得到的:
import re
strRegex = r"N[^P][ST][^P]"
objRegex = re.compile(strRegex)
print objRegex.findall('NNSTL')
这将打印:NNST
我期望的是:NNST 和 NSTL
谢谢
最佳答案
re.findall 将只返回非重叠匹配
试试这个:
>>> strRegex = r"N[^P][ST][^P]"
>>> regex = compile(strRegex)
>>> def newfind(regex,str,pos=0):
... result=regex.search(str,pos)
... if result is None: return []
... else: return [result.group()]+newfind(regex,str,result.start()+1)
...
>>>
>>> newfind(regex,'NNSTL')
['NNST', 'NSTL']
引用:https://mail.python.org/pipermail/tutor/2005-September/041126.html
关于python - 在模式中匹配 RegEx 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22030428/