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"; }
Underscores added to output for clarity.
Update: Just for the record, (?!\S) is a negative lookahead assertion. It ensures that lines are always split on the last possible space character up to the 27 character line limit. In other words, each line must contain a leading space, followed by up to 27 characters, and the next character can't be a non-space. That way the next line will always synch up with the requisite 'space' delimiter.
Dave
In reply to Re^2: splitting text into lines -- code -> regex
by davido
in thread splitting text into lines -- code -> regex
by theorbtwo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |