views:

726

answers:

10

How do I create a regular expression to accept not more than 10 digits?

thanks

+3  A: 

/\D\d{0,10}\D/ (assuming "less than" includes 0)

/\D\d{1,10}\D/ (if you want from 1 to 10 digits)

Jeremy Smyth
That is exactly 10 digits only
Hugoware
Edited to suit, thanks!
Jeremy Smyth
Jeremy please elaborate how your regex is going to work
Rakesh Juyal
\D means "not a digit", so this matches up to 9 digits surrounded by non-digits, e.g. a123b, but not 1111111111111111111.
Jeremy Smyth
Did you test that man? It doesn't work for me...
Hugoware
@HBoss: you're right - edited to fix :)
Jeremy Smyth
+1  A: 

In Perl:

^\d{,9}$

perldoc perlretut is a nice tutorial on regular expressions in Perl.

Alan Haggai Alavi
+6  A: 
^\d{1,9}$

That will match anything from 1 digit to 9.

Since you didn't specify the regex flavor you're working with, that should get you where you need to be. If not, tell us which regex technology you're using.

inkedmn
technically, not more than 10 includes 10, doesn't it?
seanmonstar
+1  A: 

I think this would do the trick:

^\d{1,10}$

Joseph
He wants LESS than 10 digits.
Zanoni
Note that that will also match the empty string, unlike \d{0,10}. By itself, this regex may have issues (since the empty string is located everywhere within a string). If the digit string is optional and this is part of a larger regex, it would be what you want.
Brian
Correct: \d{,9}
Zanoni
@Zanoni The title indicates that 10 is to be included, which is why I chose to include it. There is definitely some ambiguity between the title and the description.
Joseph
That would also match strings with >10 digits since there's no requirement for matching a non-digit after matching 1..10 digits.
laalto
@laalto Thanks! I updated it with ^ and $
Joseph
A: 

for "less than 10" and at least 1 you'd want, assuming it's the only value...

^\d{1,9}$
Hugoware
+1  A: 

/\D\d{,9}\D/ in Perl

joe
A: 

This will find at least one and no more than 9 digits in a row:

\d{1,9}

Dave Everitt
+9  A: 

Since you've asked "how", I'll try to explain step by stem. Because you did not specify which regexp flavor you are using, so I'll provide examples in PCRE and two POSIX regexp variants.

For simple cases like this you should think of regular expression as of automation, accepting one character at a time, and saying whenever it is valid one (accepting the character) or not. And after each character you can specify quantifiers how many times it may appear, like (in PCRE dialect) * (zero or more times), + (one or more time) or {n,m} (from n to m times). Then the construction process becomes simple:

PRCE  | B.POSIX | E.POSIX   | Comments
------+---------+-----------+--------------------------------------------------
^     | ^       | ^         | Unless you are going to accept anything with 10
      |         |           | digits in it, you need to match start of data;
\d    | [0-9]   | [:digit:] | You need to match digits;
{1,10}| \{1,10\}| {1,10}    | You have to specify there is from 1 to 10 of them.
      |         |           | If you also wish to accept empty string — use 0;
$     | $       | $         | Then, data should end (unless see above).

So, the result is ^\d{1,10}$, ^[0-9]\{1,10}\$ or ^[:digit:]{1,10}$ respectively.

drdaeman
+1 nice answer. I was about to comment on different regex flavours.
Kev
+1 for the explanation.
Rakesh Juyal
A: 

have a play around with http://gskinner.com/RegExr/

Ash Kim
A: 

http://www.regexbuddy.com/

but I'd suggest to divide concerns here, just check if the length of the string is <=10 characters after the input, you don't need regex to do that.

MadH