Regular expression to match numbers with semicolon with decimal places

I have this regex:

^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$

      

however it fails when I have a sum like this: 41022095.6

is there something I am missing?

0


source to share


4 answers


Your regex expects either a decimal point or a decimal point followed by two decimal digits. It depends what you want, but you can make your regex match your suggestion by doing the following:

^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{1,2})?$

      



I changed {2}

near the end to {1,2}

to give one or two decimal digits after the decimal point. I also changed the value .

to \.

because simple .

in regex means "match any character".

+3


source


doesn't mean dot means any character "unless it's in parentheses?"



+1


source


This part: "(.\d{2})?"

says that your number can optionally end with a "dot followed by two digits", but not necessarily. Because you don't have a "dot followed by two digits" anywhere in your example, and nothing else that allows a dot matches.

Also, noticed duffymo's ass, you probably want to avoid the dot so that it matches full stop instead of any character.

0


source


Try changing that last bit to (. \ D {1,2}) ?. Otherwise it will require 2 digits at the end.

Also, you may need to avoid "."

0


source







All Articles