Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Patternmatching IPaddresses

by rjt (Deacon)
on Jun 22, 2013 at 23:42 UTC ( #1040303=note: print w/ replies, xml ) Need Help??

in reply to Patternmatching IPaddresses

You might try explicitly matching either the beginning of the string or a space with (^|\s):

use 5.014; # For /r regex modifier print s/(^|\s)\d+\.\d+/$1X.X/gr for <DATA>; __DATA__

Note that if your Perl is older than 5.014, and hence you can not use the /r modifier, you can replace the print() statement with this:

    print map { s/(^|\s)\d+\.\d+/$1X.X/g; $_ } <DATA>;

If you need stricter validation of your input data, the following regexp will only match lines that have two IP addresses and nothing else:

    s/^\d+\.\d+\.(\d+\.\d+) \d+\.\d+\.(\d+\.\d+)$/X.X.$1 X.X.$2/;

Note that the /g modifier is not necessary in this case as the regexp covers the entire string.

Comment on Re: Patternmatching IPaddresses
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2015-11-30 09:58 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (769 votes), past polls