简体   繁体   English

正则表达式匹配最后一次出现

[英]regex match last occurrence

I'm trying to find ways to do it other than these two: 我试图找到除了这两个之外的方法:

# match last occurence of \d+, 24242 in this case
>>> test = "123_4242_24242lj.:"
>>> obj = re.search(r"\d+(?!.*\d)", test)
>>> obj.group()
'24242'
>>> re.findall(r"\d+", test)[-1]
'24242'

I'm sure you can find more clever regular expressions that will do this, but I think you should stick with findall() . 我相信你可以找到更多聪明的正则表达式来做到这一点,但我认为你应该坚持使用findall()

Regular expressions are hard to read. 正则表达式很难阅读。 Not just by others: let 10 days go by since the time you wrote one, and you'll find it hard to read too. 不仅仅是其他人:自从你写一篇文章以来,让我们过去10天,你会发现它也很难阅读。 This makes them hard to maintain. 这使得它们难以维护。

Unless performance is critical, it's always best to minimize the work done by regular expressions. 除非性能至关重要,否则最好尽量减少正则表达式所做的工作。 This line... 这条线......

re.findall(r"\d+", test)[-1]

... is clean, concise and immediately obvious. ......干净,简洁,立即显而易见。

这个基于前瞻性的正则表达式匹配字符串中的最后一位数字:

\d+(?=\D*$)

I'm trying to find ways to do it other than these two: 我试图找到除了这两个之外的方法:

A slight modification to your first approach. 对您的第一种方法稍作修改。 Capture the digits followed by anything that is not a digit at the end of the string. 捕获数字,然后是字符串末尾不是数字的任何数字。

>>> import re
>>> test = "123_4242_24242lj.:"
>>> print re.findall(r'(\d+)\D*$', test)
['24242']
>>>

Another alternate would be to substitute : 另一种替代方案是替代

>>> re.sub(r'.*?(\d+)\D*$', "\\1", test)
'24242'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM