Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Regex and splitting

by astronogun (Sexton)
on Apr 29, 2012 at 04:18 UTC ( #967892=perlquestion: print w/ replies, xml ) Need Help??
astronogun has asked for the wisdom of the Perl Monks concerning the following question:

Hi to all,

Im currently testing and studying regular expression with splitting. Right now I have this code:

open my $infile, "<", "test.txt" or die "cannot open input: $!"; my @infile1= <$infile>; close $infile; foreach my $dns ( @infile1 ) { chomp $dns; if ($dns =~ /(dns:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$ +/){ my ($ip, $dns1) = split /dns:/, $dns; ($ip) = split /,/, $dns1; print "$ip";} }

The test.txt contains the ff:

host1 dns:192.168.243.30,asdf host2 dns:192.168.243.1,qwert

When I run the code it doesn't displayed anything... It seems the expression didn't matched... But when I tried changing the input file with only "dns:x.x.x.x" it matched. Is there something missing with my code? Thanks so much..

Comment on Regex and splitting
Select or Download Code
Re: Regex and splitting
by Anonymous Monk on Apr 29, 2012 at 04:31 UTC
    Please explain your regex, in particular the last character $
Re: Regex and splitting
by GrandFather (Cardinal) on Apr 29, 2012 at 04:34 UTC

    Either split first and match just the IP part, or remove the $ from the end of your regular expression. The $ matches the end of the line so the total match fails because there is stuff after the IP number.

    True laziness is hard work
Re: Regex and splitting
by Kenosis (Priest) on Apr 29, 2012 at 05:45 UTC

    Although this solution doesn't use split, you can capture the ip between the colon and comma with just a regex--given your file's contents:

    use strict; use warnings; use 5.010; say /:(.*),/ while <DATA>; __DATA__ host1 dns:192.168.243.30,asdf host2 dns:192.168.243.1,qwert

    I know this doesn't specifically address your "Is there something missing with my code?" question, as the prior comments effectively have, but it may be interesting information to you.

    Hope this helps!

      Actually the one GodFather helped me by removing the $ sign and just split it if in case i want to display only the ip address. Thanks for the help really appreciate :)

Re: Regex and splitting
by jwkrahn (Monsignor) on Apr 29, 2012 at 06:31 UTC
    open my $infile, "<", "test.txt" or die "cannot open input: $!"; my @infile1= <$infile>; close $infile; foreach my $dns ( @infile1 ) { chomp $dns; if ($dns =~ /(dns:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$ +/){ my ($ip, $dns1) = split /dns:/, $dns; ($ip) = split /,/, $dns1; print "$ip";} }

    Perhaps you want something like this:

    open my $infile, "<", "test.txt" or die "cannot open input: $!"; while ( my $dns = <$infile> ) { if ( $dns =~ /dns:([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) +/ ) { print "$1"; } } close $infile;
Re: Regex and splitting
by AnomalousMonk (Monsignor) on Apr 29, 2012 at 07:29 UTC

    Regexp::Common (with the automatically loaded Regexp::Common::net) can simplify IP extraction. For one thing, it (Update: can be used to create a regex that) will not match IPs with invalid (Update: decimal) octets such as are allowed by  [0-9]{1,3}   (try different invalid octets in the  $s string).

    >perl -wMstrict -le "use Regexp::Common qw(net); ;; my $s = 'host1 dns:192.168.243.30,asdf'; my $IP = qr{ (?<! \d) $RE{net}{IPv4} (?! \d) }xms; my ($ip) = $s =~ m{ $IP }xmsg; print qq{ip '$ip'} if $ip; " ip '192.168.243.30'
Re: Regex and splitting
by 2teez (Priest) on Apr 29, 2012 at 07:38 UTC

    ofcourse using regex alone you can get your desired output, but since you said you are studying regular expression with splitting, maybe the code below could help:

    use warnings; use strict; my %hash; while(<DATA>){ chomp; my ($dns)=grep $_=>split/^.+?:/,$_; my ($ip,$dns1)=split/,/,$dns; $hash{$dns1}=$ip; } print $hash{$_},$/ foreach keys %hash; __DATA__ host1 dns:192.168.243.30,asdf host2 dns:192.168.243.1,qwert

    You could check perldoc -f split for details
    Hope this helps

Re: Regex and splitting
by Kenosis (Priest) on Apr 29, 2012 at 16:17 UTC

    Here's another regex w/split:

    use warnings; use strict; use 5.010; map{say /(.*),/} (split /:/)[1] while <DATA>; __DATA__ host1 dns:192.168.243.30,asdf host2 dns:192.168.243.1,qwert

    Actually, by studying split, your are also studying regexes, since split takes a regex.

    There are excellent solutions above, including AnomalousMonk's showing how to leverage modules, so you don't need to reinvent the (regex) wheel.

    Perl's "There's more than one way to do it" (TMTOWTDI) dictum is clearly evidenced by the many responses to your query, and it can be justifiably asserted about these that--as Adrian Monk says--"It's a blessing, and a curse."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967892]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2014-04-16 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (434 votes), past polls