php - simple regex pattern confusion -
somebody give me hand please. why getting below output
$pattern = "#([a-z])*|(chol)#"; preg_match($pattern, 'chol',$m); print_r($m) // array ( [0]=> 'chol', [1]=> 'l' )
why first sub capture matched l
character while full match chol
. may missing something. expected output below
array ( [0]=> 'chol', [1]=> 'chol' )
i don't think pattern overly complicated confuse me.
you quantified group. need quantify [a-z]
character class.
use
$pattern = "#([a-z]*)|(chol)#";
see php demo , more details @ repeating capturing group vs. capturing repeated group. in short: ([a-z]*)
match , capture 0 or more lowercase ascii letters group 1 , contain whole chunk of these letters, while ([a-z])*
match , capture 0 or more occurrences of consequent lowercase ascii letter, while overwriting each previous occurrence new one.
you might consider removing redundant capturing groups if not using them.
more, since unanchored alternation, note [a-z]*
match chol
, second alternative never match. might consider putting (chol)
first alternative more specific.
so, suggest
$pattern = "#(chol)|([a-z]*)#";
more on @ remember regex engine eager.
Comments
Post a Comment