Regex: How to match a decimal '.' if it's follow by or preceded by (or both) a '\d' decimal number
Here's what I currently have, but it only works if the decimal is preceded and followed by a decimal.
^\$?(\d*?(?<=\d)\.?(?=\d)\d*?)$
So the string: '$50.00'
matches, but '$.50'
, and '$50.'
don't match (I want them to)
I want to retrie开发者_如何学Pythonve the matched decimal like '50.00' in a single group if possible so that I can grab the normalized value if there is a match.
Try this:
^\$?(\d+(?:\.\d*)?|\.\d+)$
It will match:
^\$? # an optional $ at the begin
(\d+ # one or more digits
(?:\.\d*)? # followed by an optional decimal part
|\.\d+ # or just decimal places without any leading digits
)$
(?=^.*\d.*$)^(?:\$\s*)?(?:\d|,)*?\.?(?:\d?)*$
disallows:
$
.
$.
<empty>
<whitespace>
allows:
$50,000
$500
$0
50,000
500
0
.0
.00000000000
$50,000.000000
$ 5.
^\$?\s*((?=\d*\.|\d)\d*(?:\.\d*)?)$
Err, what about
^\$?(\d*\.?\d*)$
精彩评论