How to use regular expressions do reverse search?
问题 For example: My string is: 123456789 nn nn oo nn nn mlm nn203 . My target is: nn . Then, I match string from the end to the beginning and return the first match result and its postion. In this examlpe, the result is nn start in [-5] end in [-3]. I wrote the simple funcitonto do this process, but how to use regular expressions to do this job? 回答1: 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]