Regex for numeric check

Posted on 2007-10-04
Last Modified: 2010-04-15
The below only works if the number isn't negative or have decimals.  How would I modify it to handle such?

if (Regex.IsMatch(MyString, @"^\d+$"))
  Console.WriteLine(MyString + " is numeric");
Question by:arichexe
    LVL 14

    Accepted Solution

    if (Regex.IsMatch(MyString, @"^-?(\d+)(\.\d*)?$"))
    LVL 84

    Expert Comment

          Assuming that you don't care about IEEE notations like "NaN" or "Infin-
           ity", you probably just want to use a regular expression.

              if (/\D/)            { print "has nondigits\n" }
              if (/^\d+$/)         { print "is a whole number\n" }
              if (/^-?\d+$/)       { print "is an integer\n" }
              if (/^[+-]?\d+$/)    { print "is a +/- integer\n" }
              if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
              if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number\n" }
              if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
                                   { print "a C float\n" }
    LVL 39

    Expert Comment

    by:Pratima Pharande

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 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

    16 Experts available now in Live!

    Get 1:1 Help Now