Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for number with length of 4, 5 or 6

I need a regular expression that validate for a number with length 4, 5, 6

I used ^[0-9]{4} to validate for a number of 4, but I do not know how to include validation for 5 and 6.

like image 425
Ghyath Serhal Avatar asked Oct 23 '11 07:10

Ghyath Serhal


People also ask

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.

Which regex will match records with 4 numbers at the end of the string?

It's because of the \w+ where \w+ match one or more alphanumeric characters. \w+ matches digits as well.

What is the regular expression for phone number?

Regular expression to allow numbers like +111 123 456 789: ^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$


2 Answers

Try this:

^[0-9]{4,6}$ 

{4,6} = between 4 and 6 characters, inclusive.

like image 51
James Avatar answered Sep 20 '22 16:09

James


[0-9]{4,6} can be shortened to \d{4,6}

like image 44
Andy Mabbett Avatar answered Sep 23 '22 16:09

Andy Mabbett