use strict; use warnings; use re q{eval}; sub doSep { print q{-} x 40, qq{\n}; } my $count = 0; my $string = q{[x2]{x3}(x4)}; my $rxClose = qr@[]>})]@; my $rxOpen = qr@[[<{(]@; my $rxBetween = qr {(?x) (?<=($rxClose)) (?=($rxOpen)) (?{print qq{Match @{ [++ $count] }: left $1, right $2\n}}) }; print qq{ Before: $string\n}; doSep(); $string =~ s{$rxBetween}{+}g; doSep(); print qq{ After: $string\n}; #### Before: [x2]{x3}(x4) ---------------------------------------- Match 1: left >, right [ Match 2: left >, right [ Match 3: left ], right { Match 4: left ], right { Match 5: left }, right ( Match 6: left }, right ( ---------------------------------------- After: +[x2]+{x3}+(x4) #### use strict; use warnings; use re q{eval}; sub doSep { print q{-} x 40, qq{\n}; } my $count = 0; my $string = q{[x2]{x3}(x4)}; my $rxClose = qr@[]>})]@; my $rxOpen = qr@[[<{(]@; my $rxBetween = qr {(?x) ($rxClose) ($rxOpen) (?{print qq{Match @{ [++ $count] }: left $1, right $2\n}}) }; print qq{ Before: $string\n}; doSep(); $string =~ s{$rxBetween}{$1+$2}g; doSep(); print qq{ After: $string\n}; #### Before: [x2]{x3}(x4) ---------------------------------------- Match 1: left >, right [ Match 2: left ], right { Match 3: left }, right ( ---------------------------------------- After: +[x2]+{x3}+(x4)