Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re^3: Newbie regular expressions questionby davido (Cardinal) |
on Apr 12, 2014 at 04:48 UTC ( [id://1082054]=note: print w/replies, xml ) | Need Help?? |
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:
The output will be this:
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:
...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
In Section
Seekers of Perl Wisdom
|
|