Hello superwombat,
If you precede the expression to match with the greedy match-all .*, the match you get will be the last one:
#! perl use strict; use warnings; my $string = ' start: end: test code start: real 1 end: real with start: real repeating newlines and more than start: real one instance end: real of the start: real desired string end: real start: end:'; my $header = 'start: real'; my $footer = 'end: real'; $string =~ /.*(^$header$(?:.*)^$footer$)/ms; print "$1\n" if $1;
Output:
18:45 >perl 1078_SoPW.pl start: real desired string end: real 18:45 >
Note the /ms modifiers in the regex. As perlre explains:
Used together, as /ms, they let the "." match any character whatsoever, while still allowing "^" and "$" to match, respectively, just after and just before newlines within the string.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: Regex to find the last matching set in a long scalar
by Athanasius
in thread Regex to find the last matching set in a long scalar
by superwombat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |