Regex for match number.

Posted on 2005-05-12
Last Modified: 2010-04-16
I am trying to process a number. I would like to check if the number is a valid currency .

If the user enters

  Input                         should return
  $50000                         50000  
  $50,000                        50000  
  $50,000.12                   50000.12  
  $50000.23                    50000.23
  USD 50,000                  50000

 After it matches a regex patter for currency then i use a  Convert.ToDouble

Any ideas?

Question by:steve_mick972
    LVL 21

    Accepted Solution

    presume we have two textboxes, input and output, and a button for checking and conversion:

    private void button1_Click(object sender, System.EventArgs e)
          string input = textBox1.Text;

                    // first we check whether the format is ok, so only numbers, $-sign, ',' and '.' are acceptable
          Regex myRegex = new Regex( @"[^\d$,.]" );
          if( myRegex.IsMatch( input ))
                MessageBox.Show( "wrong format" );

                    // than we do replacement of $-sign and ',' with nothing
          myRegex = new Regex( "[$,]" );
          textBox2.Text = myRegex.Replace( input, "" );            

    LVL 7

    Expert Comment

    As a general rule, validating numbers with regular expressions isn't a very good idea. There are a lot of issues  
    LVL 7

    Expert Comment

    Shoot...sorry about that, accidentally submitted in the middle there. Let me try that again:

    As a general rule, validating numbers with regular expressions isn't a very good idea. There are a lot of issues to consider, many of which are not easy to capture in a regular expression. For example, the regular expression Yurich gives above will allow strings like "," which will cause Convert.ToDouble() to throw an exception, which I assume is what you're trying to avoid by doing the validation first. Instead of validating with a regular expression and then using Convert.ToDouble(), you should consider using Double.TryParse() (documented at,vs.80).aspx), which will attempt to parse a string into a double and return false if it fails, instead of throwing an exception. This is much more reliable and safer than trying to validate a number with a regular expression.
    LVL 3

    Expert Comment

    static double F(string S) {
                string[] Currencies = new string[] { "$", "USD" };
                string AuxStr = S.Trim();
                double RetVal = -1;

                foreach (string Currency in Currencies) {
                    if (AuxStr.StartsWith(Currency)) {
                        AuxStr = AuxStr.Substring(Currency.Length).Trim();
                try {
                    RetVal = Convert.ToDouble(AuxStr);
                } catch (FormatException) { }
                return RetVal;
    LVL 3

    Expert Comment

    Really no way!

    How can you accept a solution that admits an aberration such as ".,3.$,5$.9,$0.$," as valid input?

    That solution doesn't do ANY validity check on the format of the input as a unit. It merely restricts the valid input characters, but imposes no structure or order to them. There isn't even any numeric conversion done, so the final output for the string I quoted above gives a result of ".3.5.90.". Now what's that good for?

    Also, wasn't it a requirement that the function returned 50000 for 'USD 50,000'?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now