How to use regular expressions do reverse search?

隐身守侯 提交于 2019-12-03 12:51:11

For the string itself, just do a findall and use the last one:

import re

st='123456 nn1 nn2 nn3 nn4 mlm nn5 mlm'

print re.findall(r'(nn\d+)',st)[-1]

Prints nn5

You can also do the same thing using finditer which makes it easier finding the relevant indexes:

print [(m.group(),m.start(),m.end()) for m in re.finditer(r'(nn\d+)',st)][-1]

Prints ('nn5', 27, 30)

If you have a lot of matches and you only want the last, sometimes it makes sense to simply reverse the string and pattern:

m=re.search(r'(\d+nn)',st[::-1])
offset=m.start(1)
print st[-m.start(1)-len(m.group(1)):-m.start(1)]

Prints nn5

Thomas Fenzl

First, if you're not looking for a regular expression, string.rfind is a lot easier to get right.

You can use a regular expression by using a negative lookahead, see the documentation of re:

import re
s = "123456789 nn nn oo nn nn mlm nn203"
match = re.search("(nn)(?!.*nn.*)", s)

# for your negative numbers:
print (match.start()-len(s), match.end()-len(s))
# (-5, -3)

Idea:

  • find reversed regexp (in your case irrelevant) in reversed string
  • resulting indexes convert to negative numbers + switch start<->end

Example:

>>> import re
>>> s = "123456789 nn nn oo nn nn mlm nn203"
>>> m = re.search("(nn)", s[::-1])
>>> -m.end(), -m.start()
(-5, -3)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!