def myfindall(p, s):
found = []
i = 0
while True:
r = re.search(p, s[i:])
if r is None:
break
found.append(r.group())
i += r.start()+1
return found
seq='NNSTQ'
glyco=myfindall('N[^P][ST][^P]', seq)
Matches if ... matches next, but doesn’t consume any of the
string. This is called a lookahead assertion. For example, Isaac
(?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
findall()
不返回重叠的匹配,但是没有什么可以阻止您显式搜索它们,例如:您应该改用
(?=...)
(lookahead断言),因为只有findall
匹配项才使用字符串部分一次,means忽略重叠:这将匹配所有内容,即使它重叠。正如doc所述:
您还可以查看此项以了解更多信息:
http://regular-expressions.mobi/lookaround.html?wlr=1
相关问题 更多 >
编程相关推荐