php - Regex not matching correctly? -


i trying search subject match regular expression given in pattern not working correctly. want match number/10 postcode

for example if string "hello 3/10 hu2" want preg_match function match 3 , postcode , needs in order

the strange thing when try preg_match in opposite order hu2 3/10 works when change code around not recognize value 3.

any appreciated

the code below works order hu2 3/10 need in opposite order

<?php      preg_match('/([a-za-z]{1,2}[0-9][a-za-z0-9]?) ([0-9]{1,2})/', "hu2 3/10", $matches);          echo $matches[1];     echo "<br>";     echo $matches[2];   ?> 

if change around, have include /10 in pattern. note can change delimiters don't have escape forward slash:

preg_match('~([0-9]{1,2})/10 ([a-za-z]{1,2}[0-9][a-za-z0-9]?)~', "3/10 hu2", $matches); 

the [0-9] can replaced \d. also, if 10 variable number, that's easy take account, too:

preg_match('~(\d{1,2})/\d+ ([a-za-z]{1,2}\d[a-za-z0-9]?)~', "3/10 hu2", $matches); 

as final optimisation, use of i modifiers, let's leave out half of letters (it makes pattern case-insensitive):

preg_match('~(\d{1,2})/\d+ ([a-z]{1,2}\d[a-z0-9]?)~i', "3/10 hu2", $matches); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -