0

I want to only match the digits from a comma seperated number. Example:

123,456 should match 123 and 456
75,432,444 should match 75 and 432 and 444

My expression currently looks like this \b[0-9]+(!\,)?+\b. It matches numbers that are NOT seperated with a comma, but it doesn't select numbers that are seperacted with a coma. If I seperate numbers with a | character or a space the numbers are selected.

Splitting the string is not an option. I'm trying to learn RegEx, but I'm now stumbled at this.

anubhava
  • 761,203
  • 64
  • 569
  • 643
Gregor Menih
  • 5,036
  • 14
  • 44
  • 66
  • which language are you using? – Rohit Jain Oct 23 '13 at 18:22
  • 1
    Don't try to match the whole string, just split it by `,` and store the resulting array. – anubhava Oct 23 '13 at 18:22
  • How does it not work? What do you get? what are you looking to get? Why isn't `\d+` sufficient? – FrankieTheKneeMan Oct 23 '13 at 18:23
  • Rather than using language like "doesn't work the way I want it", please include what the result is and what you expect the result to be. – drobert Oct 23 '13 at 18:23
  • You also may consider \b[0-9]+(,[0-9]+)*\b Which means 'at least one sequence of digits, and any number (including zero) comma followed by some digits' repeated. But as other commenters have mentioned, scope and language are important here. – drobert Oct 23 '13 at 18:25
  • If you're trying to match comma, why do you have `!` in your regular expression? – Barmar Oct 23 '13 at 18:25
  • It's not a programming language, I'm trying to match it in OpenOffice Writer. @Barmar, because I'm not trying to match comma. – Gregor Menih Oct 23 '13 at 18:27
  • '!' is not "not" in regex usually, typically it's [^!] meaning 'a character not in the set "!"' – drobert Oct 23 '13 at 18:29

1 Answers1

0

You can try this regex:

\b(\d{1,3}(?:,\d{3})+)\b

It matches numbers that are separated by commas in group of three. Once you have the number in the capturing parentheses you can split on comma if you want to have its parts separated.

edi_allen
  • 1,878
  • 1
  • 11
  • 8