While working on Re: string substitution a thought came to me. Perhaps it is more of a meditation than a question, but I was intimidated by the thought of posting such trivia next to Abigail-II's The N-queens problem using pure regexes.

If \b, a zero-width assertion, doesn't care whether it's placed before or after the word it's guarding (so to speak), why should (?= ) care that it is for lookahead only? What is it that makes (?<= ) necessary, and (?= ) unable to fill both niches?

Then the concept comes to mind of the ^ and $ anchors. They're zero-width, and unlike \b, they care about which one gets used, and where. But maybe through habbit, that idiom seems to make sense and lend clarity. In other regexp flavors we see things like \< and \> to represent \b in a more placement-sensitive way. Perl seems fine with the simple \b. Wouldn't Perl be "just fine" with (?= ) filling the roll as both a zero width lookahead and a zero width lookbehind?

Then there's the other question.... why does (?<= ) have to contain fixed-length matches (ie, no quantifiers like + or *, and no lopsided alternation), while (?= ) is allowed to contain quantifiers or lopsided alternations (in other words, doesn't have to be fixed length)?

I've dug into Camel II, Owls, perlre, and perlretut but haven't found the answer. Perhaps the answer is just "that's the way it is." Is it? ;)


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

In reply to Why do zero width assertions care about lookahead/behind? by davido

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.