How to use regular expressions to do reverse search?

For example:
My line: 123456789 nn nn oo nn nn mlm nn203 .
My goal: nn .

Then I match the string from end to start and return the result of the first match and its position.
In this example, the result nn starts at the end of [-5] at [-3].
I wrote a simple funcitonto for this process, but how to use regular expressions to do the job?

+7
source share
3 answers

For the string itself, just search and use the latter:

 import re st='123456 nn1 nn2 nn3 nn4 mlm nn5 mlm' print re.findall(r'(nn\d+)',st)[-1] 

Print nn5

You can also do the same using finditer , which makes it easier to find the appropriate indexes:

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

Printing ('nn5', 27, 30)

If you have many matches and want only the latter, sometimes it makes sense to just change the line 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)] 

Print nn5

+10
source

First, if you are not looking for a regular expression, string.rfind much easier to get right.

You can use regex using negative lookahead, see re documentation:

 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) 
+2
source

Idea:

  • find the reverse regular expression (in your case, irrelevant) in the inverted string
  • total indices are converted to negative numbers + start 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) 
+2
source

All Articles