Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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 (Chancellor) 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
[GotToBTru]: a few years ago, when we had 3 dogs, the largest of them caught a chipmunk, but did not know what to do with it other than hold it in his mouth
[GotToBTru]: my wife told him to spit it out, so he did
[erix]: yeah, modern dogs are cute tjhat way :)
[erix]: except of course, the RHODESIAN LION HOUND ! :)
[Discipulus]: in roma cars are good mouser.. fortunnately no mouse at 7th floor..
[GotToBTru]: that's a name that should always be accompanied by a trumper fanfare
[GotToBTru]: trumpet, not trumper
[GotToBTru]: good thing too, Discipulus, no roads on the 7th floor either
[GotToBTru]: okay, fess up, who installed CB::Typo?
[Discipulus]: at 7th floor seagulls eat mice

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2017-11-17 20:09 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (272 votes). Check out past polls.