Extract last numbers with Regex

0

I have these two examples of strings :

/news/uk-news/commuter-who-extraordinary-row-woman-12345
/news/weird/dude-who-killed-14-extraordinary-98765.amp

I want to get only the last numbers, 12345 and 98765 , respectively.

In the middle of the string, there may be other numbers. And in the end, they may have other characters (or not) that are not numbers. What I really wanted was the last numbers, after the last hyphen - .

I tried with (\w+-)(\d+)(\W*) , but returned only a part. Can someone please help me with a correct solution?

    
asked by anonymous 09.12.2017 / 03:07

1 answer

1

This regex will only take the last number sequence (or just the last number) of the string:

(\d+)(?!.*\d)

Examples:

/news/uk-news/commuter-who-extraordinary-row-woman-12345
// retorna 12345

/news/weird/dude-who-killed-14-extraordinary-98765.amp
// retorna 98765

/qualquer15coisa-20.-s10.amp
// retorna 10

foo.1.10.foo
// retorna 10
    
09.12.2017 / 04:13