Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

First of all, thanks to all those who commented. Unfortunately, nobody really captured what my problem is, and the answers don't match the question. Usually, when this happens it's the asker's fault, so please excuse me if I didn't state my question clearly.

My question is: I need one regular expression, which matches lines in an hosts file where the name of a certain machine appears (qualified or not), and it's not associated to that machine's address or to 127.0.1.1.

BrowserUk's solution doesn't work, because it doesn't cover the case where there is something else between the hostname and the address.

cosimo's example is catching a good record, not a bad one, so it's out too.

moritz's procedure doesn't use a single regex, so that doesn't apply as well.

I was trying to solve that with negative lookbehind (match this name that it's not preceded somewhere in the line by...) but that didn't work for some reason. And, as it turned out, it was even overly complicated.

The right solution was found by oha, uses negative lookahead (instead of lookbehind), and it's much simpler: this one:

^(?!(127\.0\.1\.1|10\.20\.11\.99)\s)([a-fA-F0-9:\.]+)(\s+[a-zA-Z0-9\.\ +-]+)*(\s+kvm-test-v06(?:\.example\.com))(\s+[a-zA-Z0-9\.\-]+)*\s*

(Some extra grouping is my fault, not oha's).

I didn't realize that I could say "the beginning of the line is not followed by..." and use lookahead instead of lookbehind.

Thanks all for trying! And double thanks to oha!

Ciao!
--bronto


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

In reply to Re: Misunderstanding negative look behind by bronto
in thread 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found