my $re = qr{ (?> # don't backtrack into this group !! # either the escape character, # followed by an escape character | # or !; # escape followed by delimiter char | # or [^;\n] # a character that is neither delimiter # character or a newline )+ }x; while(<>) { chomp; $str = $_; print "$_\n"; while ($str =~ /($re)/g) { print " Chunk '$1' => "; $s = $1; $s =~ s/!!(?=(!|;))/!/g; print "$s\n"; } }