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

why doesnt
if (/^\s*(?!for all)(\w+)\s*:\s*(\w+)\s*(?!<=)/i) {
match
cidr_enable: en_core_id <= NOT(dsp_reset_n);

it's got something to do with the \s*(?!<=) bit but I cant figure it out, It's doing my head in

BTW What I really want is \s*(?![<:]=) but one step at a time

update (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: negative lookaheads doing my 'ead in
by BrowserUk (Patriarch) on Nov 08, 2002 at 11:09 UTC

    The (?!<=) means that the stuff preceeding it mustn't be followed by '<='. Your example is, so it doesn't match. Remove either or both chars or change one or both to something else and it will match.

    / ^ # start of line \s* # optional whitespace (?!for all) # NOT "for all" (\w+) # One or more word chars \s* # optional whitespace : # a colon \s* # more optional whitespace (\w+) # another set of one or more word chars \s* # and more optional whitespace (?!<=) # NOT followed by the char sequence '<=' /i

    Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy
      right
      I just re-read my original question and of course what I meant was why does it match?

      When I run the above pattern it does match

        The reason it matches is because the immediate preceeding whitespace is optional \s*. Therefore the parser can choose to say that if there is no whitespace, then the next char after the second (\w+) is a space, which isn't '<=' so therefore is can match, so it does.

        Change that to be if (/^\s*(?!for all)(\w+)\s*:\s*(\w+)\s+(?!<=)/i) { and it no longer can match, so it doesn't (if you get my drift.

        I realise that may or may not help you.

        ++Jasper below. I couldn't find a way of doing that. The caveat is that if you wanted to match

        'cidr_enable: en_core_id == NOT(dsp_reset_n) AND <= stuff;'

        your out of luck.


        Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy
Re: negative lookaheads doing my 'ead in
by Jasper (Chaplain) on Nov 08, 2002 at 11:30 UTC
    I think the regex should be:
    /^\s*(?!for all)(\w+)\s*:\s*(\w+)\s*(?!.*<=)/i
    the negative lookahead looks for the thing immediately following. It can still match if it backs off a couple of characters, unless you put in the .*.

    It seems to work anyway (ie it doesn't match now)

    I hope that was what you wanted (I'm confused now!)

    Jasper