且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中的字符串中搜索最后一次出现的正则表达式?

更新时间:2023-02-19 12:22:50

一种方法是在正则表达式前加上 (?s:.*) 并强制引擎在最远的位置尝试匹配并逐渐退缩:

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")

请注意,此方法的结果可能与 re.findall("pattern", "target_text")[-1] 不同,因为 findall 方法搜索用于非重叠匹配,并且并非所有可以匹配的子字符串都包含在结果中.

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.

例如,在 abaca 上执行正则表达式 aafindall 将返回 aba 作为唯一匹配项,并且选择它作为最后一个匹配项,而上面的代码将返回 aca 作为匹配项.

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.

另一种选择是使用 regex 包,它支持REVERSE 匹配模式.

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

结果或多或少与上述 re 包中带有 (?s:.*) 的方法相同.但是,由于我自己还没有尝试过这个包,所以不清楚在 REVERSE 模式下反向引用是如何工作的 - 在这种情况下,模式可能需要修改.

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.