Preg match all greedy with exception for string

NettaSmith

New Member
My source string is this:\[code\]{categories group_id="3"}{category_name}{/categories}{categories group_id="4"}{category_name}{/categories}\[/code\]My regex is this:\[code\]preg_match('/{categories group_id="3"}(.*){\/categories}/s', $tagdata, $matches);\[/code\]Which results in:\[code\]Array( [0] => Array ( [0] => {categories group_id="3"} {category_name} {/categories} {categories group_id="4"} {category_name} {/categories} ) [1] => Array ( [0] => {category_name} {/categories} {categories group_id="4"} {category_name} ))\[/code\]You can see that the greediness is too greedy, and it goes all the way to the end of the second instance. What I'm expecting is this:\[code\]Array( [0] => Array ( [0] => {categories group_id="3"} {category_name} {/categories} ) [1] => Array ( [0] => {category_name} ))\[/code\]
 
Back
Top