in reply to Extract Multiple Lines from the End of a multi-line string
sub Last_N_Lines { my($String, $N) = @_; my $re = '^' . ( '.*\\n' x ($N-1) ) . '(?:.*\\n|.+)'; return ( $String =~ /($re)\z/ )[0]; }
or
sub Last_N_Lines { my($String, $N) = @_; --$N; return ( $String =~ /^( (?:.*\n){$N} (?:.*\n|.+) )\z/mx )[0]; }
Update: Added alternative.
Update: Added leading anchor to reduce backtracking.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extract Multiple Lines from the End of a multi-line string
by NateTut (Deacon) on Oct 16, 2008 at 21:14 UTC | |
by ikegami (Patriarch) on Oct 17, 2008 at 00:08 UTC | |
by ikegami (Patriarch) on Oct 17, 2008 at 00:11 UTC | |
by NateTut (Deacon) on Oct 17, 2008 at 16:15 UTC | |
by ikegami (Patriarch) on Oct 17, 2008 at 16:58 UTC | |
by NateTut (Deacon) on Oct 17, 2008 at 17:03 UTC | |
|