narainhere has asked for the wisdom of the Perl Monks concerning the following question:

Hie monks, I was playing around with look-ahead and look-behind, just stumbled upon this ,
my $str="axx12yya"; if($str=~m/12(?=.*a)/) { print "match \n"; }
This matches the last 'a' in the string, Now I tried matching the first 'a' in the sting by look-behind assertion
my $str="axx12yya"; if($str=~m/12(?<=a.*)/) { print "match \n"; }
I got an error message
Variable length lookbehind not implemented before HERE mark in regex m +/12(?<=a.* ) << HERE / at test2.pl line 3.
Please explain why this error occurs and how to match the first occurrence of 'a'.One more thing noticed is if I replace the '.*' with \w{2} like
if($str=~m/12(?<=a\w{2})/)
I am not getting any error message but it fails to match.

The world is so big for any individual to conquer

Replies are listed 'Best First'.
Re: look-ahead and look behind!!
by moritz (Cardinal) on Oct 22, 2007 at 09:27 UTC
    You are using .* in your lookbehind, which can match a string of arbitrary length. This is not implemented in the regex engine.

    As to the send question: you could use m/(?<=a\w{2})12/ instead. The lookbehind starts from the current position in the string, not from the start of the regex match. Which mean that the two \w match "12", and there is an "x" where the regex engine searches for an "a". Hence there is no match.

Re: look-ahead and look behind!!
by GrandFather (Saint) on Oct 22, 2007 at 09:46 UTC

    The match string for a look behind match must match a fixed number of characters. That means that ?, *, + and {n, m} are not allowed because they can match a variable number of characters. It also means that if you use |, each clause must be the same length.

    If you really, really must use a look behind to match the first a then you could:

    $str =~ /(?<=^a)/;

    but all you actually need is /a/.


    Perl is environmentally friendly - it saves trees
      Hey monks I am not to be mis-understood over here as a person who wants to make his life complex.Actually I have some complex and confidential code that should not be posted.So I emulated the scenario with a simple yet to-the point code :)

      The world is so big for any individual to conquer

        I suspect you now have what you need. However if not then you need to give us a little more context.

        We don't mind irrelevant details being omitted from a problem description - indeed we encourage it, but make sure you leave enough detail to allow us to help solve your actual problem. To that end it often helps to tell us something of the larger problem so we can help you find the most appropriate solution.


        Perl is environmentally friendly - it saves trees
Re: look-ahead and look behind!!
by dk (Chaplain) on Oct 22, 2007 at 09:37 UTC
    Your use of look-behind will not match because 12(?<=a\w{2}) will try try to match a\w{2} tracking back right after 2, and that will only get 2 itself of course. You probably want (?<=a\w{2})12 instead.

    Also, as you've noticed, variable-length look-behinds are not implemented, so for the time being you might want to use the old good a.*12.

Re: look-ahead and look behind!!
by Anonymous Monk on Oct 22, 2007 at 09:45 UTC