|P is for Practical|
Misunderstanding negative look behindby bronto (Priest)
|on May 30, 2012 at 09:44 UTC||Need Help??|
bronto has asked for the
wisdom of the Perl Monks concerning the following question:
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:
(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:
and I must say I am quite surprised that a slightly different pattern doesn't match (or actually matches more than expected):
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:
Thanks in any case
In theory, there is no difference between theory and practice. In practice, there is.