I'm trying to come up with a regex to deal with a general scenario to capture a number from a string where the number may have one or more non-numeric characters pre/post-fixed to it.
The number can contain zero or one decimal or comma.
If the string contains multiple "sets" of consecutive digits separated by non-digits I would like the regex to fail ("sets" is probably not the correct terminology).
As an example the following inputs would succeed with a match:
abc12.00xyz
would match 12.00
0.1$
would be valid and match 0.1
.01
would be valid and match .01
123abc
would be valid and match 123
abc123
would be valid and match 123
These inputs would fail to match:
abc12.00xyz322
would fail due to the second "set" of digits, 322 in this example
12t2
would fail due to having two separate "sets" of digits
I've tried many permutations and I'm not making much headway. This is the closest I've come to so far. It matches on the numbers correctly, excluding the non-digits from the match, but it includes all "sets" of numbers in the string.
([\d]*[.,])?[\d]
Any suggestions would be appreciated.
CodePudding user response:
You can use a capture group:
^[^0-9\r\n]*?([0-9]*\.?[0-9] )[^0-9\r\n]*$
^
Start of string[^0-9\r\n]*
Optionally match any char except a digit or a newline, as few as possible([0-9]*\.?[0-9] )
Capture group 1, match optional digits, optional comma and 1 digits[^0-9\r\n]*
Optionally match any char except a digit or a newline$
End of string