my $n = # choose your rgx number # skip a new-line beginning with a literal space. my $rgx1 = qr/^ /; # skip a new-line beginning with a non-printing character # \s = space, \t = tab, \r = carriage-return, \n = new-line my $rgx2 = qr/^\s/; #### # $rgx3 is untested, but provided for example. # see note below my $rgx3 = qr/\G # Global Anchor (?<=\.\ ) # Pattern for start of sentence ( # $1 (\ +)? # Extra leading spaces ? (.*?\.) # $2 - the sentence )/x; # end $1 and regex #### my $regex = $n == 1 ? $rgx1 : $n==2 ? $rgx2 : $rgx3 ; unless( $n == 3 ){ while(){ next if $regex; tr* *,* ; # replace all spaces with a comma tr* *,*s ; # replace all squeezed spaces with a comma next; } }else{ die 'regex number choice fail' unless $n == 3; #slurp in input for multi-line parse my $input = do { local $/ = undef; }; # apply substitution operator with regex to string; $input =~ s/$rgx3/ $1 =~ /^\s/ ? $2 =~ tr* *,* : $2 /gme; # each sentence substituted, if starts with extra # leading spaces, substitutes commas. # else substitues existing sentence. # untested. thx to Cristoforo for idea of tr in e }