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 

demo


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -