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

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -