Regular expression to match last number in a string

Your regex \d+(?!\d+) says

match any number if it is not immediately followed by a number.

which is incorrect. A number is last if it is not followed (following it anywhere, not just immediately) by any other number.

When translated to regex we have:

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

Rubular Link

Leave a Comment