tags:

views:

187

answers:

5

I've got a string that may or may not contain a number of 4 or 5 digits. I'm looking for a regex that can detect if the string does in fact have such a number.

+1  A: 

Simple \d{4,5} will suffice.

Eugene Morozov
+2  A: 

Perhaps

\d{4,5}

?

Lennaert
A: 

.NET? Then it's [0-9]{4,5}

Vilx-
\d works okay in .Net, too.
Joel Coehoorn
Right. I'm not much of a Regexp guy, so I don't remember character classes. [0-9] is easier. :D
Vilx-
+2  A: 

\d{4,5} will also find strings with 6 digit numbers in - I don't know whether that's a problem or not. You might want to do something like this:

([^\d]+|^)\d{4,5}[^\d]

Will Dean
Thanks for the accept, but there are better answers than this now
Will Dean
+4  A: 

The foolproof one to avoid longer numbers would be:

([^\d]|^)\d{4,5}([^\d]|$)

I'm assuming you don't want to allow for a comma after the thousands digit? If you do then:

([^\d]|^)\d{1,2},\d{3}([^\d]|$)
David M