Solved

Regular Expressions digits

Posted on 2007-08-12
477 Views
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
Question by:igor92128

LVL 142

Expert Comment

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

LVL 62

Expert Comment

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

LVL 62

Accepted Solution

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

LVL 7

Expert Comment

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

Expert Comment

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…