$begin = '[xyz]'; $end = '[/xyz]'; $string = '[xyz]level 1.1 [xyz]level 2.1[/xyz] rest of 1.1 [/xyz]'; 1 while ($string =~ s@ \Q$begin\E ((?:(?!=\Q$end\E).)*) \Q$end\E @ "