1) foo and his friend bar 2) Stuff and more (stuff) 3) More (and30) more (and) more 4) garbage (8) 5) some other things 6) (7lalala) #### 1) foo and his friend bar2) Stuff and more (stuff)3) More (and30) more (and) more4) garbage (8)5) some other things6) (7lalala) #### $text = reverse $copy; $text =~ s/ (?<= \) ) (\d+) (?= [^()]* \) ) /$1\n/gx; $text = reverse $text; #### my $bal = # this is from perlre qr/ \( (?: (?> [^()]+ ) | (??{$bal}) )* \) /x; $text =~ s/ ( (?: (??{$bal}) [^(\d]* )* ) (\d+) (?= \) ) /$1\n$2/xg; #### $text =~ s/ (?<= \[ ) ([^\]]*) (?= \] ) / strip_slash($1) /gex; sub strip_slash { $_=pop; s/\\//g; $_; }