Python 3.x Regex获取带有负号的模式的最后一次出现

Python 3.x Regex获取带有负号的模式的最后一次出现,python-3.x,regex,Python 3.x,Regex,我一直在遵循正则表达式,以便能够得到我想要得到的模式的最后一次出现。我打算得到数字(带/不带负号)、数字和其后的任何字符 目标: aa-11aa -> gets the -11aa 11aa -> gets the 11aa aa11aa11aa -> gets the 2nd 11aa aa-11aa-11aa -> gets the 2nd -11aa aa-11a(a) -> gets the -11a(a)

我一直在遵循正则表达式,以便能够得到我想要得到的模式的最后一次出现。我打算得到数字(带/不带负号)、数字和其后的任何字符

目标:

    aa-11aa -> gets the -11aa
    11aa -> gets the 11aa
    aa11aa11aa -> gets the 2nd 11aa
    aa-11aa-11aa -> gets the 2nd -11aa
    aa-11a(a) -> gets the -11a(a)
    aa-11aa -> gets the 1aa
    11aa -> gets the 1aa
    aa11aa11aa -> get the 2nd 1aa
    aa-11aa-11aa -> get the 2nd 1aa
    aa-11a(a) -> gets the 1a(a)
然而,我只让这个模式工作,只得到一个位数和数字后面的后缀<代码>(?:*)(*\d+)(.*)$。以下是我的模式的结果:

问题:

    aa-11aa -> gets the -11aa
    11aa -> gets the 11aa
    aa11aa11aa -> gets the 2nd 11aa
    aa-11aa-11aa -> gets the 2nd -11aa
    aa-11a(a) -> gets the -11a(a)
    aa-11aa -> gets the 1aa
    11aa -> gets the 1aa
    aa11aa11aa -> get the 2nd 1aa
    aa-11aa-11aa -> get the 2nd 1aa
    aa-11a(a) -> gets the 1a(a)
有没有关于如何获得所需结果的帮助?

试试以下方法:

-?\d+[^\d]+$
它还包括第二次出现的数字,因为
表示任何字符

请注意,
\w
匹配任何单词字符(等于
[a-zA-Z0-9\

以下操作应有效

.*?(-?\d+[^\d]*)$

首先非贪婪地匹配前面的任何字符,然后在您的组中有条件地匹配减号,后跟一些数字,后跟零个或多个非数字字符

这是一个很好的尝试;但是,它无法获取数字后面的任何字符。我无法添加一个示例,其中字符串可以是
aa-11a(a)
。您想要数字后面的任何字符吗?修改了答案。它当前匹配所有事件。我只想更新最后一个。这应该可以,是的。这也行。谢谢(:.*)(-\d+)(.)美元怎么样。使第一组成为非reedy