Main Content

Regular expression for 6 digits OR 25 digits

Archive - Originally posted on "The Horse's Mouth" - 2008-04-16 19:06:00 - Graham Ellis

I can write a regular expression to match ANY number of digits between 6 and 25 ...

^[[:digit:]]{6,25}$

but how would I write a regular expression to match either 6 digits or 25 digits, but no number in between?

^([[:digit:]]{6}|[[:digit:]]{25})$

(The regular expressions in this example are Tcl and PHP/ereg types. use \d instead of [[:digit:]] for PHP/preg and Perl.)


You may wonder why I want this. A client with 6 digit or 25 digit part numbers, a year number that can be 2 or 4 digits ...