Click here to Skip to main content
14,977,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Iam Trying a Regex Expression which need to Accept only Numeric values (both Integer and Decimal with sign) and not alphabets.I tried with below expression

[-+]?[.]?(^[0-9]+[.]?^[0-9]+)$

but it doesn't work . Kindly Help

What I have tried:

[-+]?[.]?(^[0-9]+[.]?^[0-9]+)$

I tried this expression in C#
void T1_HTextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(T1_H.Text, "^[-+]?[.]?[0-9]+[.]?[0-9]{0,3}$"))
{
MessageBox.Show("Please enter only numbers.");
T1_H.Text="";
return;
Posted
Updated 10-Apr-16 23:45pm

First off, add a caret to the beginning - that forces it to consider the whole data:
^[-+]?

The Expresso library suggests
^[+-]?(?:\d+\.?\d*|\d*\.?\d+)[\r\n]*$

Which works fine for me.
I'm not at all sure what the last bit of your one is for:
^[0-9]+

But if it's for an exponent, then the caret needs to be escaped:
^[+-]?(?:\d+\.?\d*|\d*\.?\d+)\^[0-9]+$
Should do it

[edit]I HATE MARKDOWN! :mad:[/edit]
   
v3
Comments
Member 12450988 12-Apr-16 4:50am
   
Im Getting the error "Unrecognized escape sequence" and Pointing at "\." in C# Code.

Whereas this expression is working in Expresso and other validator tools.
OriginalGriff 12-Apr-16 5:05am
   
That's because '\' is an escape character in C# string handling, and it and teh following character are concatenated into a single "special" character. For example, \" inserts a double quote instead of terminating the string, and \n inserts a newline.
Either change your code to "double up" the backslashes:
string s = "hello\\.";
to create a string "hello\."
or use the @ prefix:
string s = @"hello\.";
The ^ symbol in your regex means "assert position at start of the string".

Therefore in this case you shouldn't use it in the middle of your regex - you're saying
"first allow a plus or minus"
"then allow a period ."
then the string starts

which isn't going to work. Take out the ^ symbols - except maybe put one at the very start to genuinely assert the start of the string.

You end up with:
^[-+]?[.]?([0-9]+[.]?[0-9]+)$


This regex will validate numerics with and without preceding +- symbol, and with/without a period e.g.:
-8
8.3
+2.3

Even this isn't a great regex though - because it *will* validate "0.8.8" (two periods) which isn't a valid number.

This regex:
^[+-]?[0-9]{1,9}(?:\.[0-9]{1,2})?$
will validate negative and positive decimals to 2 decimal places: taken from Stack Overflow Link


edit: wow - three solutions in the time it took me to type this :)
   
v3
Comments
Member 12450988 12-Apr-16 4:49am
   
Im Getting the error "Unrecognized escape sequence" and Pointing at "\." in C# Code.

Whereas this expression is working in Expresso and other validator tools.
glen205 12-Apr-16 5:14am
   
The backslash character is an escape character in C# strings. If you want your string literal to ... literally ... have a backslash in it you can:

1) escape the backslash with a backslash: (note there is now a double-backslash)
string foo = "^[+-]?[0-9]{1,9}(?:\\.[0-9]{1,2})?$";

2) declare the string as a string literal with the @ symbol
string foo = @"^[+-]?[0-9]{1,9}(?:\.[0-9]{1,2})?$";
I would Google for that. For instance this page looks promising c# - Regex for numbers only - Stack Overflow[^].
   
I have used this before, give it a go

^[+-]?\d*\.\d+$|^[+-]?\d+(\.\d*)?$
   
Comments
Member 12450988 12-Apr-16 4:50am
   
Im Getting the error "Unrecognized escape sequence" and Pointing at "\." in C# Code.

Whereas this expression is working in Expresso and other validator tools.
You are using the wrong concept! Number styles are localized - e.g. in Germany, I'd expect a decimal comma instead of a decimal point.
.Net number types provide a TryParse function (e.g. decimal.TryParse(...)). Use that instead.
   
This site have a feature that allow to see what does a regEx expression.
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900