Search code examples
regular-language

Show zeroes in a Regular Expression decimal


Can someone tell me how to get the zeroes to show in a regular Expression containing zeroes for decimals.

For Example 1,320.00 When I turn it to a Regular Expression the .00 disappears. I need them to show. Here is the formula I was working with.

(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\.\d*$) 

Any help would be appreciated.

thanks,


Solution

  • Here's a pattern which will capture the full number, including commas and decimals:

    ^(\d{1,3},)*\d{1,3}\.\d\d$
    

    The first group, (\d{1,3),), will match groups of one to three digits followed by a comma. It is followed by a *, so the pattern will match 0 or more of these groups (i.e. it will still match 320.00 and 12,312,122.00).

    The second part, \d{1,3}\., will match the 1-3 digits preceding the decimal point.

    Finally, \d\d$ match the two decimal points. It looked like you're trying to match US currency, so I hard-coded in 2 digits for readability, but if you need to match, say, one or more decimal points, try this:

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

    Here's a demo.