-2

So I have a scenario where I need to extract out all numbers from this string, now the problem that I am encountering is that there could be white-spaces between the commas or the actual number themselves. How can I write a generic regex to extract out only numbers. My current scenario looks like:

Test strings:

<classifier id="box-geometry" value="476,736,703,997" />
<classifier id="box-geometry" value="476,736,703, 997" />
<classifier id="box-geometry" value="476, 736, 703, 997" />

Attempted regex: value="(\d+),(\d+),(\d+),(\d+)"

Example: https://regex101.com/r/se0dwk/1/

As you can see, only the first string matches for all the numbers in their respective capturing groups.

Any help would be appreciated.

Thanks!

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54

1 Answers1

2

Your regex pattern is missing optional whitespace. Try including that:

value="(\d+),\s*(\d+),\s*(\d+),\s*(\d+)"

Check the demo below.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360