#!/usr/local/bin/perl use strict; while (my $str = ) { chomp($str); $str =~ s/(\d+[.,]\d+ # Substitute a (simple) number ... | # or ... [^.,\d ]+) # something without periods, comma's, # digits or spaces ... ((?!$)[.,]?) # followed by a period or comma, if # possible, but not when at the end of # the line ... /$1$2 /gx; # by the number or string, followed by # the found period or comma, and a # trailing space print "<$str>\n"; } __DATA__ 1,2,a,b a,1,b,2, 1,21,a,b,word,d.3,4.5.6 __END__ <1,2, a, b> <1,21, a, b, word, d. 3,4. 5.6>