Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex, exclude number starting with certain character

Tags:

java

regex

I want to find number with don't proceed with £ character. Say I have a string:

"Gross Domestic Product in 2012 was £127 billion"

I want to return 2012 only.

I ve tried [^$][0-9]* but this only ignores $ character. Tried to match the whole word which starts and ends with number \b[0-9]*\b but \b applies only to [0-9a-zA-Z]

Any more suggestions?

like image 264
Alex Avatar asked Dec 10 '25 23:12

Alex


1 Answers

One option which works (as tested using http://gskinner.com/RegExr) is:

(?<![£0-9])[0-9]+

This uses negative lookbehind to exclude £. The additional exclusion of 0-9 is to prevent it finding "27" in the above in addition to the "2012".

like image 166
Stuart Golodetz Avatar answered Dec 12 '25 11:12

Stuart Golodetz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!