Esteemed Monks

I am trying to solve a problem which seems to require negative lookbehind regexes, which I never used. And, based on the results I am getting, it looks like I don't understand a **** of it :) I hope you can easily point out where my reasoning is failing.

I have a "normalized" hosts file; each line matches one of the following patterns:

^\s*$ (empty line) ^\s*#.*$ (comment line) ^[a-fA-F0-9:\.]+(\s+[a-zA-Z0-9\.-]+)+\s*$ (host record)

(The third regex could be narrowed down quite a bit, but let's KISS).

Now, in order to delete spurious lines, I would like to delete all those records which contain the host name (qualified or unqualified), but don't start with the right address of the machine (either the "local" version, 127.0.1.1 or, say, 10.20.11.99).

I tried several ways to match a whole line with no success, so I tried to start over simple. So far, I got only a partial check working (partial as in: covers only one, very specific subcase), namely:

(?<!10\.20\.11\.99\s)kvm-test-v06.+

and I must say I am quite surprised that a slightly different pattern doesn't match (or actually matches more than expected):

(?<!10\.20\.11\.99\s)kvm-test-v06.*

All other attempts to match a whole line, containing fully-qualified/unqualified name of the host somewhere, but preceded by an address other than 127.0.1.1 or 10.20.11.99 failed badly.

I am sure I am missing something fundamental and stupid, but I can't see that. Can you help me find it out?

A sample hosts file to test against:

127.0.0.1 localhost 10.20.11.99 kvm-test-v06.example.com kvm-test-v06 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters # Spurious records 1.2.3.4 litter trash kvm-test-v06.example.com garbage 1.2.3.5 kvm-test-v06 more garbage 1.2.3.6 litter kvm-test-v06 1.2.4.7 kvm-test-v06.example.com kvm-test-v06

Thanks in any case

Ciao!
--bronto


In theory, there is no difference between theory and practice. In practice, there is.
pmsig

In reply to Misunderstanding negative look behind by bronto

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.