Answer a question

In python, I can easily search for the first occurrence of a regex within a string like this:

import re
re.search("pattern", "target_text")

Now I need to find the last occurrence of the regex in a string, this doesn't seems to be supported by re module.

I can reverse the string to "search for the first occurrence", but I also need to reverse the regex, which is a much harder problem.

I can also iterate to find all occurrences from left to right, and just keep the last one, but that looks awkward.

Is there a smart way to find the rightmost occurrence?

Answers

One approach is to prefix the regex with (?s:.*) and force the engine to try matching at the furthest position and gradually backing off:

re.search("(?s:.*)pattern", "target_text")

Do note that the result of this method may differ from re.findall("pattern", "target_text")[-1], since the findall method searches for non-overlapping matches, and not all substrings which can be matched are included in the result.

For example, executing the regex a.a on abaca, findall would return aba as the only match and select it as the last match, while the code above will return aca as the match.


Yet another alternative is to use regex package, which supports REVERSE matching mode.

The result would be more or less the same as the method with (?s:.*) in re package as described above. However, since I haven't tried the package myself, it's not clear how backreference works in REVERSE mode - the pattern might require modification in such cases.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐