use strict; use warnings; my $text = " R12345678-A1234567 INCL.EUR 3,31 MWST JULI MONATL. GEB HR T-DSL FLAT 01.07.04-31.07.04 12,34"; while ( $text =~ / (.{0,27}(?!\S))/g ) { my $out = $1 . ( '_' x ( 27 - length( $1 ) ) ); print "$out\n"; }