Link to home
Create AccountLog 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
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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" }