in reply to Regex text extraction b/w first intance of pattern X and third instance of pattern Y.

Unlike the two previous posters, I won't make the mistake of assuming you are using 'a', 'b' and 'c' here in another role as placeholders. In particular, I presume 'c' here to be some string, which can even be longer than a single character.

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?:

my $index2 = -1; foreach (1 .. 3) { $index2 = index($body, "c", $index2 + 1); die "No third 'c'" if $index2 < 0; }
Then use pos() to find where 'a' finished matching (or use index() as well):
$body =~ /a/g or die "No first 'a'"; my $index1 = pos($body); # my $index1 = index($body, 'a') + length('a');
Then grab what's in between using the offsets:
my $inbetween = substr($body, $index1, $index2 - $index1);
A few things are left as an exercise to the reader:
  • Comment on Re: Regex text extraction b/w first intance of pattern X and third instance of pattern Y.
  • Select or Download Code

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
    I won't make the mistake of assuming you are using 'a', 'b' and 'c' here

    Why is it a mistake to assume that he is doing what he says he is doing, and what his example shows he is doing?

    I presume 'c' here to be some string, which can even be longer than a single character.

    A why is it better for you to presume that he means something other than what he says?

    I think that your re-interpretation of the question asked, is a valid and interesting adjunct to it, but why the baseless snide narrative?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      BrowserUK:

      I think the "expanded" problem is reasonable given the title, since the title specifies patterns X and Y rather than specific characters. The way I read the OP is that (s)he properly simplified it to a trivial case in the example.

      ...roboticus

        I agree, though the use of index isn't going to handle "patterns" in the "Regex" sense of the term. But absense the narrative, it would have had my upvote.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
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
    If it's not just c, my solution still works with the equivalent of [^c] for arbitrary patterns. But since I don't know what pattern that might be, I stuck to what the OP said rather than inventing stuff up.