in reply to A bug in regex with conditional subpattern?

The condition (?=b) must return false.

It does. The condition returns false, therefore it make no attempt to match the (null) yes pattern (which would have matched anyway), and simply moves on to the next element 'a$', which matches.

In other words, if the condition of the conditional expression, in this case the lookahead assertion, is false, then no attempt is made to use either the yes pattern, or the no pattern (here missing). In effect, the entire construct becomes a noop.


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.
  • Comment on Re: A bug in regex with conditional subpattern?

Replies are listed 'Best First'.
Re^2: A bug in regex with conditional subpattern?
by moritz (Cardinal) on Mar 17, 2011 at 10:43 UTC
    and simply moves on to the next element.

    And that's the bug, IMHO. If the yes-pattern is present, and the condition fails, the whole match normally fails. Why should it move on if the match fails? Compare:

    'a' =~ /^xa/ # match of x fails, doesn't move on to match a

    When the no-pattern is not present, and the condition fails, it normally doesn't match:

    $ perl -wE 'say "a" =~ /^(?(?=b)xxx)a/' $ # no match

    So it doesn't show the "move on"-behavior when the yes-pattern is something else than the empty pattern.

    So it's a special case. And it's not documented. So it's a bug.

      Why should it move on if the match fails?

      Is "(?(condition)yes-pattern)" an abbreviation for "(?(condition)yes-pattern|)" (i.e. with an explicitly empty no-pattern)? This seems most reasonable to me, but I find nothing in the documentation to suggest whether this or any other possibile behaviour should be expected.

      The most fundamental "bug" seems to be lack of documentation of intended behaviour.

Re^2: A bug in regex with conditional subpattern?
by jethro (Monsignor) on Mar 17, 2011 at 10:38 UTC

    "In effect, the entire construct becomes a noop. "

    In which case the first line of Serge314's example should match, shouldn't it?