php - How do write reg exp for string has structure like a-b-1 and a-b-1/d-e-2? -
sorry english not good. have problem, want compare 2 different string use reg exp. first string has structure a-b-1, ex: mobile-phone-1. , second string has structure a-b-1/d-e-2, ex: mobile-phone-1/nokia-asha-23. how can it? can use preg_match() method or method ... method 2 different string. much!
code demo:
if (preg_match("reg exp 1", string1)) { // } if (preg_match("reg exp 2", string2)) { // }
p/s: shouldn't care code demo
$pattern = '#[\w]+-[\w]+-[\d]+(/[\w]+-[\w]+-[\d]+)?#'; if (preg_match($pattern, $str, $matches)){ return $matches; }
to match shorter string use:
$pattern = '#[\w]+-[\w]+-[\d]+#';
to match longer:
$pattern = '#[\w]+-[\w]+-[\d]+/[\w]+-[\w]+-[\d]+#';
to match longer more dashes:
$pattern = '#[\w]+-[\w]+-[\d]+/[\w-]+[\d]+#';
Comments
Post a Comment