• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

Regular Expressions digits

How do I write a regular expression that takes any number that can have digits or a "-" sign in front of it?
I want to accept all numbers:

4545353
-6566376

...

I need to modify this: Regex.IsMatch(input, "^[0-9]*$")
0
igor92128
Asked:
igor92128
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should be what you need:
Regex.IsMatch(input, "^([-])?([0-9])+$")

the - 0 or 1 times (?)
the 0-9 symbols 1 or multiple times (+)

0
 
Fernando SotoRetiredCommented:
Hi igor92128;

This should do what you want.

using System.Text.RegularExpressions;

String input = "-23456";
if( Regex.IsMatch( input, "^-?\d+$") )
{
    // Then it is a number in the form of nn... or -nnn...
}
else
{
    // It is not a number at all
}

Fernando
0
 
Fernando SotoRetiredCommented:
Hi igor92128;

Explanation of my pattern.

The Regex pattern, "^-?\d+$" has the following meaning.

^      Pattern matching starts at the beginning of the string
-?     Looking for the first character to be the minus sign or for it to not be there at all.
        The ? is a quantifier stating that the previous character must be there zero or one time.
\d    The next character must be a digit.
+     The plus sign is a quantifier stating that the previous character must repeat one or more times.
$     This has the meaning that we must be at the end of the string.

Because the pattern is using the Regex meta-characters ^ and $ if the data is coming from user input then you should use Trim on the string before testing. For example:

String input = " -23456 ";   // This will not match the Regex pattern because of the spaces at the beginning and end of the string.
input = input.Trim();           // The Regex pattern will now work.

Fernando
0
 
icrCommented:
An alternative would be:
int surplusInt;
bool isNumber = int.TryParse(input, out surplusInt);

Trouble with this is that you have an integer that you aren't actually using (that is, if you don't plan on using it). However, I do think it is clearer in it's intentions than the regex and it runs faster with less memory overhead.
0
 
wiscarzCommented:
Try this code:

using System.Text.RegularExpressions;
.
.
Regex rx = new Regex(@"\s{0,}[\+|-][0-9]+\s{0,}$");
            if (rx.IsMatch(" +98787 "))
            {
                MessageBox.Show("Successful Match!!");
            }
            else
            {
                MessageBox.Show("Failed to Match!!");
            }

norice there's no spaces between the sign and the digit, if u wanna, just put \s{0.} petween the sign and the digits expression..
and notice that a sign without a digit is not acceptable in this expression..


hope that will do..
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now