Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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__ 10.128.99.190 10.128.100.100 1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4 100.100.100.100 200.200.200.200

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040303]
help
Chatterbox?
[holli]: "die Kleine" is a (somewhat diminishing) term for woman :) also someone at stock overflow seems to think i'm female
[Discipulus]: holli orange is gold in the morning, silver in afternoon and lead in the nigth..
[Discipulus]: banana too better not in the night
[holli]: it actually feels more like a lead-acid battery ^^
[Discipulus]: erix are you now travelling in space? iirc you were in low countries
choroba had 4 beers (or was it 5?) yesterday, plus a pickled cheese
[choroba]: megalag :-(
[erix]: ok, I looked it up, should have been: "Haribo macht Kinder froh und Erwachsene ebenso"
[Discipulus]: also mixing pizza (fermented/levited ) with juices is not so good

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-11-22 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (316 votes). Check out past polls.

    Notices?