user2423959
user2423959

Reputation: 834

regex to capture only number at the end

I've some sample text as below.

this is 1989 representing text 245
this is sample text 235
this is 234 sample text with comma seperator 345,756

here i need a regex to get number only at the end, I've tried the below

[(0-9)+]

but this is capturing all the numbers, but only the number after the text ends needs to be captured.

The captured values should be

245
235
345,756

please let me know how can i do this.

Thanks

Upvotes: 1

Views: 113

Answers (4)

Pradeep Kumar
Pradeep Kumar

Reputation: 6979

Isn't this much simpler ?

Allowed: only digits

\d+$

Allowed: digits, comma, dashes

[\d,-]+$

Allowed: digits, dashes

[\d-]+$

Upvotes: 1

Hybris95
Hybris95

Reputation: 2400

You can also try this which is simple to read :
^.*[0-9]+$

^.* Begins with whatever you want of whatever length
[0-9]+$ Ends with at least one number (or more)

If you wish only to recover the numbers and not the lines that correspond to your request :
[0-9]+$

And here we can add the complexity of recovering the comma :
[0-9]+(,[0-9]+)?$

Upvotes: 1

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12807

You can use

\d+(?=[-, \d]*$)

Regular expression visualization

Demo

Upvotes: 1

sshashank124
sshashank124

Reputation: 32197

You can do it as:

/[\d,]+$/mg

DEMO

To allow more characters, just add them to the character class:

/[\d,.-_]+$/mg

or if you want, simply allow everything except non accented letters:

/[^a-zA-Z]+$/mg

Upvotes: 4

Related Questions