Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple integer regular expression

Tags:

regex

asp.net

I have ValidationRegularExpression="[0-9]" which only allows a single character. How do I make it allow between (and including) 1 and 7 digits? I tried [0-9]{1-7} but it didn't work.

like image 494
Johnson Avatar asked Apr 14 '11 08:04

Johnson


People also ask

What is the simplest regular expression?

When simple regular expression syntax is being used, most characters, except metacharacters are treated as literal characters and match only themselves (for example, "a" matches "a", "(bc" matches "(bc", etc). Operators. Operator.

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.

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.


1 Answers

You got the syntax almost correct: [0-9]{1,7}.

You can make your solution a bit more elegant (and culture-sensitive) by replacing [0-9] with the generic character group "decimal digit": \d (remember that other languages might use different characters for digits than 0-9).

And here's the documentation for future reference:

  • .NET Framework Regular Expressions
like image 92
Heinzi Avatar answered Oct 19 '22 23:10

Heinzi