regex - Java Regular Expression to match dollar amounts -
this i've been using
\$?[0-9]+\.*[0-9]*
but when doing testing noticed things like
$$$34.00
would return match (but matcher.group()
) returns matched substring. don't want pass regular expression if user enters more 1 dollar sign tried this:
\${1}[0-9]+\.*[0-9]*
but seems behave same regular expression first typed. right i'm testing in java but, plan use in c++ using boost libraries. please don't give me solution here because i'm trying learn without giving me answer.
but need making user can enter 1 dollar sign (which thought \${1}
do)
since you're doing learn regex...
^\$(([1-9]\d{0,2}(,\d{3})*)|(([1-9]\d*)?\d))(\.\d\d)?$
breakdown:
^\$
start of string $ single dollar sign
([1-9]\d{0,2}(,\d{3})*)
1-3 digits first digit not 0, followed 0 or more occurrences of comma 3 digits
or
(([1-9]\d*)?\d)
1 or more digits first digit can 0 if it's digit
(\.\d\d)?$
period , 2 digits optionally @ end of string
matches:
$4,098.09 $4098.09 $0.35 $0 $380
does not match:
$098.09 $0.9 $10,98.09 $10,980456
Comments
Post a Comment