use strict; use warnings; my ($quote_on,$output); $_ = q/this is a "test of some regexp" blah blah this is another "test of some regexp" foo bar/; for (split //) { $quote_on = ($quote_on ? 0 : 1) if (/\"/); s/\s/_/g if $quote_on; $output .= $_; } print "output: $output\n";