Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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:,asdf host2 dns:,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..

Replies are listed 'Best First'.
Re: Regex and splitting
by GrandFather (Sage) 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 AnomalousMonk (Canon) 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:,asdf'; my $IP = qr{ (?<! \d) $RE{net}{IPv4} (?! \d) }xms; my ($ip) = $s =~ m{ $IP }xmsg; print qq{ip '$ip'} if $ip; " ip ''
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 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:,asdf host2 dns:,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 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:,asdf host2 dns:,qwert

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

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 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:,asdf host2 dns:,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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967892]
Approved by GrandFather
[Corion]: marioroy: Oh, that's always cool, having API-compatible modules. This makes testing and comparing things much easier
[marioroy]: IPC in MCE::Shared can handle 400k (sends) per second. That's seems a lot for being a pure-Perl module. After making the release, will come back and post a solution for a node by a fellow wanting faster logging.
[Corion]: While working on WWW::Mechanize:: Chrome, I had the suspicion that AnyEvent was doing something wrong, but I was able to swap it out for Mojolicious and the error persisted.
[Corion]: Of course, the error was in my own code ;)
[marioroy]: Corion, start and start_child in MCE::Hobo::Manager return a MCE::Hobo object, whereas P::FM returns the PID. I can have it return the PID though. I tried Hobo::Manager with several P::FM modules, just changed P::FM to MCE::Hobo::Manager and it works.
[marioroy]: I also have a Hobo driver for Forklift allowing folks to use in multiple classes, no conflicts with one another. That's not possible for P::FM.
[Discipulus]: congrats marioroy!
[marioroy]: CORE::wait works well eventhough multiple instances or classes using Hobo::Manager.
[Corion]: marioroy: I'm not sure what the normal use for the PID is in P:FM, but I guess that most programs just ignore or log it
[Corion]: Oh, yes, programs could call wait $pid, but if your $pid is an object, then you could add a ->wait method to it and wait $pid would call that automatically "thanks" to indirect object notation

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-05-26 08:40 GMT
Find Nodes?
    Voting Booth?