Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for 10 digit number without any special characters

Tags:

c#

.net

regex

What is the regular expression for a 10 digit numeric number (no special characters and no decimal).

like image 743
Grasshopper Avatar asked Jan 13 '11 21:01

Grasshopper


People also ask

What is the regular expression for numbers only?

To check for all numbers in a field To get a string contains only numbers (0-9) we use a regular expression (/^[0-9]+$/) which allows only numbers. Next, the match() method of the string object is used to match the said regular expression against the input value.

What does the regex 0 9 ]+ do?

In this case, [0-9]+ matches one or more digits. A regex may match a portion of the input (i.e., substring) or the entire input. In fact, it could match zero or more substrings of the input (with global modifier). This regex matches any numeric substring (of digits 0 to 9) of the input.

How do you write numbers in regular expressions?

The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.

Is there a regex for anything not a letter?

replace(/[^\w]/g, ' ') .


2 Answers

Use this regular expression to match ten digits only:

@"^\d{10}$" 

To find a sequence of ten consecutive digits anywhere in a string, use:

@"\d{10}" 

Note that this will also find the first 10 digits of an 11 digit number. To search anywhere in the string for exactly 10 consecutive digits and not more you can use negative lookarounds:

@"(?<!\d)\d{10}(?!\d)" 
like image 147
Mark Byers Avatar answered Oct 02 '22 02:10

Mark Byers


Use the following pattern.

^\d{10}$ 
like image 41
A_Nabelsi Avatar answered Oct 02 '22 04:10

A_Nabelsi