Saturday, 15 June 2013

php - regex not working, accepting almost all letter number combinations -



php - regex not working, accepting almost all letter number combinations -

can tell me i'm doing wrong? accepting match.

if (preg_match("/^[a-z][a-z][a-z][0-9]|[1-9][0-9]|1[0-4][0-9]|15[0-1]:[0-9]|[1-9][0-9]|1[0-6][0-9]|17[0-6]/", $_get['id'])) { echo "match"; } else { echo "no match"; }

i'm wanting match if 1st letter capital a-z, 2nd letter little letter a-z, 3rd letter little letter a-z, number between 1 , 150, colon :, number between 1 , 176. should match abc150:176 zyx1:1 not abc151:177

use this:

^[a-z][a-z]{2}(?:[1-9][0-9]?|1[0-4][0-9]|150):(?:[1-9][0-9]?|1[0-6][0-9]|17[0-6])$

see demo.

^ asserts @ origin of string [a-z][a-z]{2} matches 1 upper-case , 2 lower-case letters (?:[1-9][0-9]?|1[0-4][0-9]|150) matches number 1 150 : matches colon (?:[1-9][0-9]?|1[0-6][0-9]|17[0-6]) matches number 1 176 $ asserts @ end of string

in php:

$regex = "~^[a-z][a-z]{2}(?:[1-9][0-9]?|1[0-4][0-9]|150):(?:[1-9][0-9]?|1[0-6][0-9]|17[0-6])$~"; echo (preg_match($regex,$string)) ? "***match!***" : "no match";

php regex

No comments:

Post a Comment