Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top