I need help extracting numbers from a string like:
There are 1,000 people in those 3 towns.
I want to create an array like
["1,000", "3"]
.
I found a number matching Regex from Justin in this question
^[+-]?(\d*|\d{1,3}(,\d{3})*)(\.\d+)?\b$
Although it works for checking if it is a number, it needs modification to work on sentences. Specifically, the removal of "start" and "end" markers (^ and $).
See the regex patterns at: regex101 with start/end defined regex101 without start/end defined
Removing the start and end definitions results in unnecessary 0-length matches, but it also splits numbers with commas in them.
How can I modify the regex (or create a new one) to successfully extract numbers from sentences, including those with commas?
An added challenge would be avoiding the occurrence of 0 length matches.