Which prints:my $str = 'xbaaaaaa' . ('_' x 20) . 'bcccccccccc'; my $start = $str =~ /a+/ ? $+[0] : 0; my $end = $str =~ /b/ ? $-[0] : 0; my $ext = substr $str, $start, $end - $start; $, = " | "; $\ = "\n"; print $start, $end, $ext;
8 | 1 | ____________________bccc
You need to continue the second search where the first one left off. Adding the //g switch to both regexps can do that, provided you make sure pos is clear before you start on the first one. A failed match can take care of that. I'm not sure that's absolutely necessary, but you never know... It depends of what you matched on $str before, and on whether pos gets properly localised to the current block, by perl. (Note: it doesn't make a difference if you do it or not for this particular string, but I'm trying to cover all possibilities, in general. I want to make sure the first regexp always starts searching from the start of the string.)
resulting in:my $str = 'xbaaaaaa' . ('_' x 20) . 'bcccccccccc'; $str =~ /(?!)/g; # A match that always fails, resetting pos() my $start = $str =~ /a+/g ? $+[0] : 0; my $end = $str =~ /b/g ? $-[0] : 0; my $ext = substr $str, $start, $end - $start; $, = " | "; $\ = "\n"; print $start, $end, $ext;
8 | 28 | ____________________
In reply to Re: Re: print data between two regular expressions
by bart
in thread print data between two regular expressions
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |