in reply to Regex text extraction b/w first intance of pattern X and third instance of pattern Y.
Sometimes, it's easier to do things without a complicated regexp. Why not use index to find a starting position of the third appearance of 'c' first?:
Then use pos() to find where 'a' finished matching (or use index() as well):my $index2 = -1; foreach (1 .. 3) { $index2 = index($body, "c", $index2 + 1); die "No third 'c'" if $index2 < 0; }
Then grab what's in between using the offsets:$body =~ /a/g or die "No first 'a'"; my $index1 = pos($body); # my $index1 = index($body, 'a') + length('a');
A few things are left as an exercise to the reader:my $inbetween = substr($body, $index1, $index2 - $index1);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex text extraction b/w first intance of pattern X and third instance of pattern Y.
by BrowserUk (Patriarch) on Mar 31, 2010 at 10:56 UTC | |
by tye (Sage) on Mar 31, 2010 at 14:09 UTC | |
by roboticus (Chancellor) on Mar 31, 2010 at 14:43 UTC | |
by BrowserUk (Patriarch) on Mar 31, 2010 at 17:15 UTC | |
|
Re^2: Regex text extraction b/w first intance of pattern X and third instance of pattern Y.
by ikegami (Patriarch) on Mar 31, 2010 at 15:34 UTC | |
by tye (Sage) on Mar 31, 2010 at 15:53 UTC |