in reply to print data between two regular expressions
After a successful match the $+[0] variable indicates the offset into the original string that the expression stopped matching at. Similarly the $-[0] variable is going to tell you the offset of the start of the expression match. If you then combine those two pieces of information with substr() you'll be able to extract all the text between your two expression matches.
my $str = 'a' . ('_' x 100) . 'b'; my $start = $str =~ /a/ ? $+[0] : 0; my $end = $str =~ /b/ ? $-[0] : 0; my $ext = substr $str, $start, $start - $end; print $ext;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: print data between two regular expressions
by bart (Canon) on Oct 08, 2003 at 09:02 UTC | |
by diotalevi (Canon) on Oct 08, 2003 at 15:50 UTC | |
|
Re: Re: print data between two regular expressions
by davis (Vicar) on Oct 08, 2003 at 08:47 UTC |