Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Newbie regular expressions question

by davido (Cardinal)
on Apr 12, 2014 at 04:48 UTC ( [id://1082054]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Newbie regular expressions question
in thread Newbie regular expressions question

You still haven't explained what rules should be followed. One could easily just write a regexp that matches exactly your input string, and only that input string. Or one could write a regexp that matches any input string containing what appears to be an IP address. One will be way too restrictive, and one too permissive. I'd like to get it right, but you're not helping.

So my wild guess is that you just want to replace any IP address that is nested between the literal text, "<ip-compute-", and "-internal>". Here's an example that accomplishes that:

use strict; use warnings; use Regexp::Common 'net'; my $input = <<'EOI'; <yoda yoda yoda> <ip-compute-10.10.10.1-internal> <yadda yadda yadda> <some more stuff> EOI my $want = <<'EOW'; <yoda yoda yoda> <ip-compute-10.10.25.18-internal> <yadda yadda yadda> <some more stuff> EOW my $replacement_ip = '10.10.25.18'; $input =~ s/(<ip-compute-)(?:$RE{net}{IPv4})(-internal>)/$1$replacemen +t_ip$2/; print $input eq $want ? "Success:\n" : "Failure:\n"; print "\t$_\n" for split /\n/, $input;

The output will be this:

Success: <yoda yoda yoda> <ip-compute-10.10.25.18-internal> <yadda yadda ya +dda> <some more stuff>

I used Regexp::Common's ::net extension to generate the portion of the regular expression that matches an IPv4 address. I did this because I didn't want to use a naive regexp such as (?:\d{1,3}\.){3}\d{1,3} only to find that it works most of the time, but matches some things that couldn't be valid IP's occasionally, and because I didn't want to trouble myself or yourself with the pain of coming up with a more robust pattern on my own.

If you're not allowed to use a module, the regular expression generated by $RE{net}{IPv4} is this:

(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[ +0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0 +-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))

...which is precisely why I didn't want to reinvent it myself. ;)

It seems possible you're dealing with XML, so you might find an XML parsing module to be a more robust solution in the longrun (though the learning curve might be more to begin with). Also, even if this solution I've provided works for you, if you plan to do this more than once, you owe it to yourself, your employer, and those people scattered across the Internet who will help you, to spend an hour reading perlretut.


Dave

Replies are listed 'Best First'.
Re^4: Newbie regular expressions question
by AnomalousMonk (Archbishop) on Apr 12, 2014 at 17:32 UTC

    Further to davido's post:
    bayareamonk: You say you have Perl 5.10 available (which provides \K), so here's a small simplification:

    c:\@Work\Perl\monks>perl -wMstrict -le "use Regexp::Common 'net'; ;; my $input = qq{<yoda yoda> <ip-compute-10.10.10.1-internal> <yadda ya +dda> \n} . qq{<more stuff>} ; print qq{[[$input]] \n}; ;; my $want = qq{<yoda yoda> <ip-compute-10.10.25.18-internal> <yadda ya +dda> \n} . qq{<more stuff>} ; ;; my $replacement_ip = '10.10.25.18'; ;; $input =~ s{ <ip-compute- \K $RE{net}{IPv4} (?= -internal>) } {$replacement_ip}xmsg; ;; print $input eq $want ? 'Success:' : 'Failure:'; print qq{[[$input]]}; " [[<yoda yoda> <ip-compute-10.10.10.1-internal> <yadda yadda> <more stuff>]] Success: [[<yoda yoda> <ip-compute-10.10.25.18-internal> <yadda yadda> <more stuff>]]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found