[c#] Regex for numbers only

I haven't used regular expressions at all, so I'm having difficulty troubleshooting. I want the regex to match only when the contained string is all numbers; but with the two examples below it is matching a string that contains all numbers plus an equals sign like "1234=4321". I'm sure there's a way to change this behavior, but as I said, I've never really done much with regular expressions.

string compare = "1234=4321";
Regex regex = new Regex(@"[\d]");

if (regex.IsMatch(compare))
{ 
    //true
}

regex = new Regex("[0-9]");

if (regex.IsMatch(compare))
{ 
    //true
}

In case it matters, I'm using C# and .NET2.0.

This question is related to c# regex

The answer is


Perhaps my method will help you.

    public static bool IsNumber(string s)
    {
        return s.All(char.IsDigit);
    }

This works with integers and decimal numbers. It doesn't match if the number has the coma thousand separator ,

"^-?\\d*(\\.\\d+)?$"

some strings that matches with this:

894
923.21
76876876
.32
-894
-923.21
-76876876
-.32

some strings that doesn't:

hello
9bye
hello9bye
888,323
5,434.3
-8,336.09
87078.

If you need to tolerate decimal point and thousand marker

var regex = new Regex(@"^-?[0-9][0-9,\.]+$");

You will need a "-", if the number can go negative.


^\d+$, which is "start of string", "1 or more digits", "end of string" in English.


Regex for integer and floating point numbers:

^[+-]?\d*\.\d+$|^[+-]?\d+(\.\d*)?$

A number can start with a period (without leading digits(s)), and a number can end with a period (without trailing digits(s)). Above regex will recognize both as correct numbers.

A . (period) itself without any digits is not a correct number. That's why we need two regex parts there (separated with a "|").

Hope this helps.


Regex regex = new Regex ("^[0-9]{1,4}=[0-9]{1,4]$")


Use beginning and end anchors.

 Regex regex = new Regex(@"^\d$");

Use "^\d+$" if you need to match more than one digit.


Another way: If you like to match international numbers such as Persian or Arabic, so you can use following expression:

Regex = new Regex(@"^[\p{N}]+$");

To match literal period character use:

Regex = new Regex(@"^[\p{N}\.]+$");

While non of the above solutions was fitting my purpose, this worked for me.

var pattern = @"^(-?[1-9]+\d*([.]\d+)?)$|^(-?0[.]\d*[1-9]+)$|^0$|^0.0$";
return Regex.Match(value, pattern, RegexOptions.IgnoreCase).Success;

Example of valid values: "3", "-3", "0", "0.0", "1.0", "0.7", "690.7", "0.0001", "-555", "945465464654"

Example of not valid values: "a", "", " ", ".", "-", "001", "00.2", "000.5", ".3", "3.", " -1", "--1", "-.1", "-0", "00099", "099"


If you need to tolerate decimal point and thousand marker

var regex = new Regex(@"^-?[0-9][0-9,\.]+$");

You will need a "-", if the number can go negative.


Another way: If you like to match international numbers such as Persian or Arabic, so you can use following expression:

Regex = new Regex(@"^[\p{N}]+$");

To match literal period character use:

Regex = new Regex(@"^[\p{N}\.]+$");

Here is my working one:

^(-?[1-9]+\\d*([.]\\d+)?)$|^(-?0[.]\\d*[1-9]+)$|^0$

And some tests

Positive tests:

string []goodNumbers={"3","-3","0","0.0","1.0","0.1","0.0001","-555","94549870965"};

Negative tests:

string []badNums={"a",""," ","-","001","-00.2","000.5",".3","3."," -1","--1","-.1","-0"};

Checked not only for C#, but also with Java, Javascript and PHP


^\d+$, which is "start of string", "1 or more digits", "end of string" in English.


Perhaps my method will help you.

    public static bool IsNumber(string s)
    {
        return s.All(char.IsDigit);
    }

Regex regex = new Regex ("^[0-9]{1,4}=[0-9]{1,4]$")


It is matching because it is finding "a match" not a match of the full string. You can fix this by changing your regexp to specifically look for the beginning and end of the string.

^\d+$

^\d+$, which is "start of string", "1 or more digits", "end of string" in English.


It is matching because it is finding "a match" not a match of the full string. You can fix this by changing your regexp to specifically look for the beginning and end of the string.

^\d+$

Your regex will match anything that contains a number, you want to use anchors to match the whole string and then match one or more numbers:

regex = new Regex("^[0-9]+$");

The ^ will anchor the beginning of the string, the $ will anchor the end of the string, and the + will match one or more of what precedes it (a number in this case).


Sorry for ugly formatting. For any number of digits:

[0-9]*

For one or more digit:

[0-9]+

While non of the above solutions was fitting my purpose, this worked for me.

var pattern = @"^(-?[1-9]+\d*([.]\d+)?)$|^(-?0[.]\d*[1-9]+)$|^0$|^0.0$";
return Regex.Match(value, pattern, RegexOptions.IgnoreCase).Success;

Example of valid values: "3", "-3", "0", "0.0", "1.0", "0.7", "690.7", "0.0001", "-555", "945465464654"

Example of not valid values: "a", "", " ", ".", "-", "001", "00.2", "000.5", ".3", "3.", " -1", "--1", "-.1", "-0", "00099", "099"


Your regex will match anything that contains a number, you want to use anchors to match the whole string and then match one or more numbers:

regex = new Regex("^[0-9]+$");

The ^ will anchor the beginning of the string, the $ will anchor the end of the string, and the + will match one or more of what precedes it (a number in this case).


It is matching because it is finding "a match" not a match of the full string. You can fix this by changing your regexp to specifically look for the beginning and end of the string.

^\d+$

Your regex will match anything that contains a number, you want to use anchors to match the whole string and then match one or more numbers:

regex = new Regex("^[0-9]+$");

The ^ will anchor the beginning of the string, the $ will anchor the end of the string, and the + will match one or more of what precedes it (a number in this case).


Here is my working one:

^(-?[1-9]+\\d*([.]\\d+)?)$|^(-?0[.]\\d*[1-9]+)$|^0$

And some tests

Positive tests:

string []goodNumbers={"3","-3","0","0.0","1.0","0.1","0.0001","-555","94549870965"};

Negative tests:

string []badNums={"a",""," ","-","001","-00.2","000.5",".3","3."," -1","--1","-.1","-0"};

Checked not only for C#, but also with Java, Javascript and PHP


If you need to check if all the digits are number (0-9) or not,

^[0-9]+$

1425 TRUE

0142 TRUE

0 TRUE

1 TRUE

154a25 FALSE

1234=3254 FALSE


Your regex will match anything that contains a number, you want to use anchors to match the whole string and then match one or more numbers:

regex = new Regex("^[0-9]+$");

The ^ will anchor the beginning of the string, the $ will anchor the end of the string, and the + will match one or more of what precedes it (a number in this case).


If you want to extract only numbers from a string the pattern "\d+" should help.


 console.log(/^(0|[1-9][0-9]*)$/.test(3000)) // true

Regex for integer and floating point numbers:

^[+-]?\d*\.\d+$|^[+-]?\d+(\.\d*)?$

A number can start with a period (without leading digits(s)), and a number can end with a period (without trailing digits(s)). Above regex will recognize both as correct numbers.

A . (period) itself without any digits is not a correct number. That's why we need two regex parts there (separated with a "|").

Hope this helps.


 console.log(/^(0|[1-9][0-9]*)$/.test(3000)) // true

Sorry for ugly formatting. For any number of digits:

[0-9]*

For one or more digit:

[0-9]+

If you want to extract only numbers from a string the pattern "\d+" should help.


Use beginning and end anchors.

 Regex regex = new Regex(@"^\d$");

Use "^\d+$" if you need to match more than one digit.


I think that this one is the simplest one and it accepts European and USA way of writing numbers e.g. USA 10,555.12 European 10.555,12 Also this one does not allow several commas or dots one after each other e.g. 10..22 or 10,.22 In addition to this numbers like .55 or ,55 would pass. This may be handy.

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

If you need to check if all the digits are number (0-9) or not,

^[0-9]+$

1425 TRUE

0142 TRUE

0 TRUE

1 TRUE

154a25 FALSE

1234=3254 FALSE


I think that this one is the simplest one and it accepts European and USA way of writing numbers e.g. USA 10,555.12 European 10.555,12 Also this one does not allow several commas or dots one after each other e.g. 10..22 or 10,.22 In addition to this numbers like .55 or ,55 would pass. This may be handy.

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

It is matching because it is finding "a match" not a match of the full string. You can fix this by changing your regexp to specifically look for the beginning and end of the string.

^\d+$

^\d+$, which is "start of string", "1 or more digits", "end of string" in English.