Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How regex works in array mode?

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

Hi, Once again I would like to seek for help..

It's simple I would like to know how regex works in perl in array mode.

I have this code wherein it will display the word ip: and the url x.x.x.x I have two inputed in my @lines and both are should be matched.. but the problem is I'm getting different result. Here's a simple code that I'm currently testing right now.

use strict; use warnings; my @lines = ("ip:192.168.243.1", "ip:192.168.243.2" =~ /(ip:([0-9]{3}| +[0-9]{2}|[0-9]{1})\.([0-9]{3}|[0-9]{2}|[0-9]{1})\.([0-9]{3}|[0-9]{2}| +[0-9]{1})\.([0-9]{3}|[0-9]{2}|[0-9]{1}))/); print "@lines\n";

and the output is:

ip:192.168.243.1 ip:192.168.243.2 192 168 243 2

Suppose to be the "ip:192.168.243.1 ip:192.168.243.2" part is the one what I want it to be except for the next one "192 168 243 2"

I tried putting m and the regex code but it will just display "ip:192.168.243.1" even if the other value in the array is also matched (ip:192.168.243.2)

I tested the regex via http://regexpal.com/ and its working fine (I think hehe!) Hope you could help me learn how this regex works :) thank you so much

Comment on How regex works in array mode?
Select or Download Code
Re: How regex works in array mode?
by JavaFan (Canon) on Apr 26, 2012 at 07:06 UTC
    The first value is just the first value of @lines. The rest are return values of the match -- which returns each subpattern matched by a set of () in list context. If you want only the first one returned, get rid off all the capturing parens except the outer ones.
      Hmm.. How will I get rid all the capturing parens? Sorry for the noob question. still learning this whole regex thing...
        How will I get rid all the capturing parens?
        An easy way, one that should also speed up your pattern, make it more understandable, and saves typing, is to replace
        ([0-9]{3}|[0-9]{2}|[0-9]{1})
        with
        [0-9]{1,3}
        Of course, you could also use the Regexp::Common module: but be aware, unlike your pattern, the one in Regexp::Common rejects invalid IP addresses.
Re: How regex works in array mode?
by jwkrahn (Monsignor) on Apr 26, 2012 at 08:42 UTC

    There is no such thing as "array mode" in Perl.    I believe you are refering to "list context"?

    ([0-9]{3}|[0-9]{2}|[0-9]{1}) could be written more simply as [0-9]{1,3} and it will also avoid the unnecessary capturing parentheses.

    use strict; use warnings; my @lines = ("ip:192.168.243.1", "ip:192.168.243.2" =~ /(ip:([0-9]{3}| +[0-9]{2}|[0-9]{1})\.([0-9]{3}| +[0-9]{2}|[0-9]{1})\.([0-9]{3}|[0-9]{2}|[0-9]{1})\.([0-9]{3}|[0-9]{2}| +[0-9]{1}))/); print "@lines\n";

    It looks like you want something like:

    use strict; use warnings; my @lines = ( "ip:192.168.243.1", "ip:192.168.243.2" ); for ( @lines ) { print "$1\n" if /(ip:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3 +})/; }
      Hi,

      Yes that's the output what i want it to be, you got me thanks :)

      My next question is.. is the 2 value on the array stored at $1? What if I want to use the value the "ip:192.168.243.2" how can i stored that at the other string for example $2 or something. Thanks

      Or something like if the items in the array are matched with the regex (like the code that you made. It will stored at $ipline1 = ip:192.168.243.1 and $ipline2 = ip:192.168.243.2 So that I can choose which one to output or print... Is it possible? Thanks

Re: How regex works in array mode?
by brx (Pilgrim) on Apr 26, 2012 at 09:09 UTC
    #!perl my $s = "how regex works in list context?"; my @capt= $s =~ /(((.)..) (.(.)...) ...(.)(.) .. (.(.))..( )....(.).(. +)(.))/; # 123 4 5 6 7 8 9 10 11 12 + 13 # h ow r e gex wor k s in l i st cont e x t + ? unshift @capt,'nothing'; print @capt[1,10,2,13,13,13,10,8,6,5,10,12,3,9,7]; print "\n"; #print "$_: $capt[$_]\n" for (0..13); __END__ how regex works in list context? how??? like this

    Read: perlre ("Capture groups").

    Each opening parens creates a new capture. Uncomment last line if you want to see each group

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-10-01 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls