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??

You never show where exactly in a regex you insert your look-behind, so I can only get what you're doing wrong.

If you put it at the start of a regex, you need to a negative look-ahead, because the IP address is not yet matched. Example:

use strict; use warnings; use 5.010; my $localhost = qr/(?!10\.20\.11\.99\s)/; while (<DATA>) { next if /^\s*$/; next if /^\s*#.*$/; if (/^$localhost([a-fA-F0-9:\.]+)(\s+[a-zA-Z0-9\.-]+)+\s*$/) { say "IP: $1; HOST: $2"; } else { chomp; say "No match for '$_'"; } } __DATA__ 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

Produces the output:

IP: 127.0.0.1; HOST: localhost No match for '10.20.11.99 kvm-test-v06.example.com kvm-te +st-v06' IP: ::1; HOST: ip6-loopback IP: fe00::0; HOST: ip6-localnet IP: ff00::0; HOST: ip6-mcastprefix IP: ff02::1; HOST: ip6-allnodes IP: ff02::2; HOST: ip6-allrouters IP: 1.2.3.4; HOST: garbage IP: 1.2.3.5; HOST: garbage IP: 1.2.3.6; HOST: kvm-test-v06 IP: 1.2.4.7; HOST: kvm-test-v06

Note that this uses the negative look-ahead directly after the ^ anchor, so that the look-ahead never backtracks.

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

It is much easier to do such a check after you have parsed the line, and extracted host name and IP address.


In reply to Re: Misunderstanding negative look behind by moritz
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 having a coffee break in the Monastery: (4)
As of 2024-03-28 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found