Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

regular expression assistance

by notasaint (Initiate)
on May 09, 2008 at 20:52 UTC ( [id://685770]=perlquestion: print w/replies, xml ) Need Help??

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

I think I'm just down to the regular expression I need for finding the line in the hosts file

192.168.1.45 hostname.domain.com hostname

so it can be replaced with the new values. Unfortunately, I am bound by the modules that already exist within the Perl build on the laptops and that does not include Regex so I can't use $RE{net}{IPv4}. In the following segment of the script I have the expression to verify that there are four octets and that they are in the correct potential ranges:

if( $vmServerIP =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/ +){ if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255){ open (my $oldhfile, ,'<', $hfilepath) or die "Could not open $hf +ilepath for reading."; my @hfile = <$oldhfile>;

However, I'm having issues with formulating the regular expression in the foreach loop if anyone could provide me with some guidance.

foreach $hfileline (@hfile) { next unless $hfileline =~/^\s*\s+$hNames/; $hfileline = "$vmServerIP $hNames\n"; }

TIA

-nas

Replies are listed 'Best First'.
Re: regular expression assistance
by Fletch (Bishop) on May 09, 2008 at 21:12 UTC

    Regexp::Common is pure Perl. If you can put your code on the boxen in question, you can put it on the boxen in question as well (or at the very least cut and paste the desired portions into your code).

    Additionally: \s*\s+ doesn't really make a whole frell of a lot of sense . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: regular expression assistance
by markkawika (Monk) on May 09, 2008 at 22:02 UTC
    I think perhaps you mean:
    next unless $hfileline =~/^\S*\s+$hNames/;
    You want to match a "hosts" line where you have an IP (\S*) followed by whitespace (\s+) followed by the hostname. Perhaps a better regex would be:
    next unless $hfileline =~/^[\d.]+\s+$hNames/;
    To force matches against real lines, and not match against commented-out entries. Another way to match an IP address without using "<= 255" is:
    if( $vmServerIP =~ m/^(?:(?:\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))\.){3 +}(?:\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))/ ){

      Great information. Thank you very much!

      -nas
Re: regular expression assistance
by ww (Archbishop) on May 09, 2008 at 21:12 UTC
    Perl without regex capability? Sounds unlikely. What do you mean?

      I'm sorry. I mean that the following is not working with the perl interpreter we have on the laptop...

      use Regexp::Common qw /net/;

      perhaps I have it wrong?

        In that case, see Fletch's answer.

        Since I gather from your prior thread that getting access to the laptops may be awkward, consider his cut'n'paste suggestion.

Re: regular expression assistance
by GrandFather (Saint) on May 10, 2008 at 00:40 UTC

    An interesting version is:

    my $fail; if ($str =~ /\b(?>(\d{1,3})(??{$fail ||= $1 > 255})\.?){4}/ && ! $ +fail) { # matched ... }

    Perl is environmentally friendly - it saves trees
      Sadly that pattern misinterprets 100100100100 as a valid IP.

      At the very least you'd want to replace \.? with (?:\.|\z) or the like.

          -- Chip Salzenberg, Free-Floating Agent of Chaos

        There are many ways of breaking the pattern. Most of them can't be resolved without knowing the nature of the strings being matched.

        The intent was to demonstrate a way of including the range test in the match rather than to strictly match IP number strings and nothing else. So don't be sad, learn from the technique and be happy. ;)


        Perl is environmentally friendly - it saves trees
Re: regular expression assistance
by carol (Beadle) on May 10, 2008 at 22:21 UTC
    I am bound by the modules that already exist within the Perl build on the laptops
    Regex is open source. You can use its code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://685770]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found