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


In reply to Re^3: Newbie regular expressions question by davido
in thread Newbie regular expressions question by bayareamonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.