in reply to substitute space to 0

Update: Unfortunately, neither of these approaches handles leading four-blank groups properly. Oh, well... (Update: Also, it seems to me that at some point the OPed example data changed from having groups of ASCII 0x20 blanks to having tabs. Is this really so? Only yueli711 can know. :)

One way (assuming a "space" is a blank, i.e., ASCII 0x20):

c:\@Work\Perl>perl -wMstrict -le "my $s = '1 2 2 5 4'; $s =~ s{ ([ ]{4}) (?! \d) }{${1}0}xmsg; print qq{'$s'}; ;; my $t = '1 2 0 0 2 0 5 0 0 0 4'; die 'bad transformation' unless $s eq $t; " '1 2 0 0 2 0 5 0 0 0 4'

Update: A slight variation if you have Perl version 5.10+ and the  \K regex operator:

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '1 2 2 5 4'; $s =~ s{ [ ]{4} \K (?! \d) }{0}xmsg; print qq{'$s'}; ;; my $t = '1 2 0 0 2 0 5 0 0 0 4'; die 'bad transformation' unless $s eq $t; " '1 2 0 0 2 0 5 0 0 0 4'


Give a man a fish:  <%-{-{-{-<