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 }