>perl -wMstrict -le "use 5.010; ;; my $s = q{xx AND(random text) xx OR(more (and more)) AND (or (more (too))))}; ;; my $intro = qr{ AND | OR }xms; my $nested_parens = qr{ ( \( ( (?: [^()]*+ | (?-2))* ) \) ) }xms; ;; while ($s =~ m{ ($intro) \s* $nested_parens }xmsg) { print qq{\$1 '$1' \$2 '$2' \$3 '$3'}; } " $1 'AND' $2 '(random text)' $3 'random text' $1 'OR' $2 '(more (and more))' $3 'more (and more)' $1 'AND' $2 '(or (more (too)))' $3 'or (more (too))'