Alex
Alex

Reputation: 503

Regex, exclude number starting with certain character

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?

Upvotes: 3

Views: 6218

Answers (4)

dinesh707
dinesh707

Reputation: 12582

This is a good tool where you can try those out. Here is a sample way to do what you asked http://regex101.com/r/iH9uS8

Upvotes: 1

user278064
user278064

Reputation: 10170

This works for me:

 (?:^|\\s)(\\d+)(?:\\s|$)

Upvotes: 0

mas.morozov
mas.morozov

Reputation: 2736

You can prefix your string with space and search for the regex \s[0-9]+ wich would mean 'digital prefix of word, starting with digit' This will exclude all words starting with $, £ or everything else except digit.

Upvotes: 1

Stuart Golodetz
Stuart Golodetz

Reputation: 20616

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".

Upvotes: 4

Related Questions