Reputation: 1578
I'm trying to write a regex that captures all the numbers in a string BUT only if the string ends with numbers.
I worked out the pattern would require a repeating capture group:
^(\D*(\d+))+$
So
My problem is that it seems that in repeated capture groups you only get the last match returned to you. (demo)
Can anyone show me where I'm going wrong?
Upvotes: 3
Views: 134
Reputation: 784998
You may use this regex with a lookahead:
\d+(?=(?:\w+\d)?\b)
RegEx Breakdown:
\d+
: Match 1+ digits(?=
: Start Lookahead assertion
(?:\w+\d)?
: Optionally match 1 or more word characters followed by a digit\b
: Word boundary)
: End Lookahead assertionUpvotes: 2