TyForHelpDude TyForHelpDude - 2 years ago 94
PHP Question

regular expression missing pattern

I try to create a regex(PHP) that allow all digits,and allow only closed brackets with [digit-digit] format for exp.

0[1-3]xxx xx //valid
[1-3]xxx xx //valid
05[1-3]xxx xx //valid
0[1-3]555 6789 //valid
0332 555 6789 //valid

0[1-11]xxx xx //not valid
[1-3]xxxa xx //not valid

my regex pattern [0-9]?[[0-9]-[0-9]][0-9 x]+ validates followings

[0-5]212xxx xx xx
[0-5]212xx xx xx
[0-5]212xx xx xx

but its not validates this string:


Change regex completely or edit the regex already but it seems getting comlicated.. How can I achieve my goal with most efficient way?

Answer Source

This pattern tests that string contains digit, construction [digit-digit], space and character x

 ^(\d|\[\d-\d\]|x| )+$

or you can write it so:

^([\dx ]|\[\d-\d\])+$

demo and explanation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download