use strict; $_=<<'_quote_'; hai xtest "aa xx aax" baix "xx" x "axa\"x\\" xa "x\\\ \\"x" ax xbai!x _quote_ print "Original:\n", $_, "\n"; s/ ( (?: # at the beginning of the string match till inside the quotes ^(?&outside_quote) " # or continue from last match which always stops inside quotes | (?!^)\G ) (?&inside_quote) # eat things up till we find what we want ) \r?\n # the thing we want to replace ( (?&inside_quote) # eat more possibly till end of quote # if going out of quote make sure the match stops inside them # or at the end of string (?: " (?&outside_quote) (?:"|\z) )? ) (?(DEFINE) (? [^"]*+ ) # just eat everything till quoting starts (? (?:[^"\\\r\n]++|\\.)*+ ) # handle escapes ) /$1\n$2/xg; print "Replaced:\n", $_, "\n"; #### my $file="testdata.csv"; open(FILE, $file) or die "Can't open $file: $!\n"; select((select(FILE), $/ = undef)[0]); $_=