Search code examples
c#regexcurrency-formatting

Regex for currency value


I have this regex that is working perfect to match currency values for U.S.A format ($1.50):

Regex money = new Regex(@"\w\^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");

I'd like to have some help to UNDERSTAND how to build a regex like that, I need to change the format for my country, like change the $ for R$.

I'm looking on msdn topics but nothing worked so far...


Solution

  • Your question has three parts, and to me it sounds like it is mostly about "learning how to fish", which is great.

    **A. The Regex You Want **

    Based on the comments, you are looking for this (see demo):

    ^R\$\d+(?:\.\d{3})*,\d{2}$
    

    B. Explanation of the Regex

    This is a relatively simple regex, and for this you can read an automatically-generated explanation. Several sites do this. Here is one (it will display better on the original site).

    NODE                     EXPLANATION
    --------------------------------------------------------------------------------   \w                       word characters (a-z, A-Z, 0-9, _)
    --------------------------------------------------------------------------------   \^                       '^'
    --------------------------------------------------------------------------------   \$                       '$'
    --------------------------------------------------------------------------------   (                        group and capture to \1:
    --------------------------------------------------------------------------------
        \d{1,3}                  digits (0-9) (between 1 and 3 times
                                 (matching the most amount possible))
    --------------------------------------------------------------------------------
        (                        group and capture to \2 (0 or more times
                                 (matching the most amount possible)):
    --------------------------------------------------------------------------------
          \,                       ','
    --------------------------------------------------------------------------------
          \d{3}                    digits (0-9) (3 times)
    --------------------------------------------------------------------------------
        )*                       end of \2 (NOTE: because you are using a
                                 quantifier on this capture, only the
                                 LAST repetition of the captured pattern
                                 will be stored in \2)
    --------------------------------------------------------------------------------    |                        OR
    --------------------------------------------------------------------------------
        (                        group and capture to \3:
    --------------------------------------------------------------------------------
          \d+                      digits (0-9) (1 or more times
                                   (matching the most amount possible))
    --------------------------------------------------------------------------------
        )                        end of \3
    --------------------------------------------------------------------------------   )                        end of \1
    --------------------------------------------------------------------------------   (                        group and capture to \4 (optional
                               (matching the most amount possible)):
    --------------------------------------------------------------------------------
        \.                       '.'
    --------------------------------------------------------------------------------
        \d{2}                    digits (0-9) (2 times)
    --------------------------------------------------------------------------------   )?                       end of \4 (NOTE: because you are using a
                               quantifier on this capture, only the LAST
                               repetition of the captured pattern will be
                               stored in \4)
    --------------------------------------------------------------------------------   $                        before an optional \n, and the end of the
                               string
    

    C. How Can I Learn to Build a Regex Like That

    Here are the resources I recommend.

    1. Books: Mastering Regular Expressions (3rd Ed), the Regex Cookbook

    2. Websites: regular-expressions.info, RexEgg, FAQ on SO

    3. Tools: RegexBuddy (commercial but outstanding debugger), regex101.com, Debuggex.com