0

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...

sshashank124
  • 31,495
  • 9
  • 67
  • 76
PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90

2 Answers2

2

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

Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105
  • If I drop the **R** *before* the **\$** and leave it like: *Regex money = new Regex(@"\w\^R\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");*. And I try to use the *isMatch* with the value *R$1,50* it still returning me false. WHy? – PlayHardGoPro May 20 '14 at 05:33
  • @PlayHardGoPro Several typos in what you sent. This is cleaner `^R\$(\d{1,3}(,\d{2})*|(\d+))?$` :) If you play with it in regex101.com you can see the results right away – zx81 May 20 '14 at 05:37
  • @PlayHardGoPro Here's a [demo](http://regex101.com/r/nG6sX3) By the way the part after the `|` allows you to match more digits if there is no comma, is that what you want? – zx81 May 20 '14 at 05:38
  • In USA you use currency format like *$1.50*. I need a regex to identify this format: *R$1,50*, *R$25,50*, *R$1.250,50*. – PlayHardGoPro May 20 '14 at 05:42
  • @PlayHardGoPro Okay, thanks for explaining the exact format you need. I have made the regex for you and added it at the top of my answer. Please let me know if it works for you. :) [demo](http://regex101.com/r/jP6fG8) – zx81 May 20 '14 at 05:57
  • Worked PERFECT !! Thank you for everything... I'll study a little deeper this Regex. Really thank your for answering me and explaining it! – PlayHardGoPro May 20 '14 at 06:03
  • You're very welcome, it was a pleasure. :) If you start studying regex and want to see something cool, bookmark [this one](http://stackoverflow.com/questions/23589174/match-or-replace-a-pattern-except-in-situations-s1-s2-s3-etc/23589204#23589204) for later, I'm really happy about this answer. :) – zx81 May 20 '14 at 06:05
1

You can just add a R in your regex as so:

Regex rmoney = new Regex(@"\w\^R\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • Is there a tutorial/article or something that could make me understand how to build one ? I jsut don't understand these slashes brackets etc... ;s – PlayHardGoPro May 20 '14 at 03:29