c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my @lines = ( 'A line with an_underscore.', 'A line with_two_underscores.', '~A line with an_underscore starting with a tilde.', '~A line with_two_underscores starting with a tilde.', ); ;; for my $line (@lines, @ARGV) { print qq{'$line'}; $line =~ s{ (?: \G (?! \A) | \A ~) .*? \K _ }{+}xmsg; print qq{'$line' \n}; } " "___" "~___" 'A line with an_underscore.' 'A line with an_underscore.' 'A line with_two_underscores.' 'A line with_two_underscores.' '~A line with an_underscore starting with a tilde.' '~A line with an+underscore starting with a tilde.' '~A line with_two_underscores starting with a tilde.' '~A line with+two+underscores starting with a tilde.' '___' '___' '~___' '~+++'