in reply to Extract Multiple Lines from the End of a multi-line string
Outputs:use strict; my $re_single = qr/ (?:\A|[\r\n]) # either the start of text or newline ( [^\r\n]* # any non-newlines (?:\z|(?:\r\n|\n\r|\r|\n)\z) # last newline or eof ) /xs; sub Last_N_Lines { my ( $str, $n ) = @_; my $nless = ( $n - 1 ); my $re_multiple = qr/ (?:\A|[\r\n]) # either the start of text or newline ( (?: [^\r\n]* (?:\r\n|\n\r|\r|\n) ){0,$nless} ) # have to enclose above so it all goes into $1 ( [^\r\n]* (?:\z|(?:\r\n|\n\r|\r|\n)\z) # last nl or eof ) /xs; if ( $n > 1 ) { if ( $str =~ m/$re_multiple/ ) { return( "$1$2" ); } else { return( "" ); } } if ( $str =~ m/$re_single/ ) { return( $1 ); } else { return( "" ); } }; my $String = "This is line 1 This is Line 2 This is Line 3 This is Line 4 This is Line 5 This is Line 6 This is Line 7 This is Line 8 This is Line 9 This is Line 10"; print("Last\[5\] Lines.\[\n" . Last_N_Lines($String,5) . "\]\n");
Last[5] Lines.[ This is Line 6 This is Line 7 This is Line 8 This is Line 9 This is Line 10]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extract Multiple Lines from the End of a multi-line string
by ikegami (Patriarch) on Oct 17, 2008 at 04:09 UTC | |
by monarch (Priest) on Oct 19, 2008 at 12:03 UTC | |
by ikegami (Patriarch) on Oct 19, 2008 at 17:01 UTC |