Link to home
Start Free TrialLog in
Avatar of arichexe
arichexe

asked on

Regex for numeric check

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");
ASKER CERTIFIED SOLUTION
Avatar of Jai S
Jai S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
      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" }