This is due to the nature of * and while it is a greedy quantifier, it is also quite a lazy quantifier. In this case \s* doesn't match any space, since it doesn't have to, and subsequently " MILE" is matched which doesn't match "MILE" so it's a successful match. Hopefully this quick oneliner and it's output should make that explanation clearer
shell> perl -Mre=debug -e '$_="1/2 MILE"; m{ 1/2 \s* (?!MILE) }x' Freeing REx: `,' Compiling REx ` 1/2 \s* (?!MILE) ' size 11 first at 1 1: EXACT <1/2>(3) 3: STAR(5) 4: SPACE(0) 5: UNLESSM[-0](11) 7: EXACT <MILE>(9) 9: SUCCEED(0) 10: TAIL(11) 11: END(0) anchored `1/2' at 0 (checking anchored) minlen 3 Guessing start of match, REx ` 1/2 \s* (?!MILE) ' against `1/2 MILE'.. +. Found anchored substr `1/2' at offset 0... Guessed: match at offset 0 Matching REx ` 1/2 \s* (?!MILE) ' against `1/2 MILE' Setting an EVAL scope, savestack=3 0 <> <1/2 MILE> | 1: EXACT <1/2> 3 <1/2> < MILE> | 3: STAR SPACE can match 1 times out of 32767... Setting an EVAL scope, savestack=3 4 <1/2 > <MILE> | 5: UNLESSM[-0] 4 <1/2 > <MILE> | 7: EXACT <MILE> 8 <1/2 MILE> <> | 9: SUCCEED could match... failed... 3 <1/2> < MILE> | 5: UNLESSM[-0] 3 <1/2> < MILE> | 7: EXACT <MILE> failed... 3 <1/2> < MILE> | 11: END Match successful! Freeing REx: ` 1/2 \s* (?!MILE) '

HTH

_________
broquaint


In reply to Re: regex negative lookahead behaviour by broquaint
in thread regex negative lookahead behaviour by shemp

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.