php - Get all possible matches in regex -
for example have following string:
(hello(world)) program
i'd extract following parts string:
(hello(world)) (world)
i have been trying expression (\((.*)\))
(hello(world))
.
how can achieve using regular expression
a regular expression might not best tool task. might want use tokenizer instead. can done using regex, using recursion:
$str = "(hello(world)) program"; preg_match_all('/(\(([^()]|(?r))*\))/', $str, $matches); print_r($matches);
explanation:
( # beginning of capture group 1 \( # match literal ( ( # beginning of capture group 2 [^()] # character not ( or ) | # or (?r) # recurse entire pattern again )* # end of capture group 2 - repeat 0 or more times \) # match literal ) ) # end of group 1
Comments
Post a Comment