Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
laziness, impatience, and hubris
 
PerlMonks  

Re: Parenthesis grouping into regexes.

by BillKSmith (Friar)
on Apr 25, 2012 at 22:10 UTC ( #967191=note: print w/ replies, xml ) Need Help??


in reply to Parenthesis grouping into regexes.

You can solve your immediate problem by using non-capturing parenthesis in the definitions of $ip4 and $ip6. Refer to the section (?pattern) in perlre


Comment on Re: Parenthesis grouping into regexes.
Re^2: Parenthesis grouping into regexes.
by BillKSmith (Friar) on Apr 26, 2012 at 03:31 UTC

    Here is an example with data made up to match your regular expressions. Are you sure about the fourth colon in $ip6?

    use strict; use warnings; my $ip4 = qr !(?: [0-9]{1,3}\.){3} [0-9]{1,3} !x; my $ip6 = qr !(?: [a-f0-9]{4}:)+ !x; my $prefix = qr ! / \d \d !x; my $pattern = qr ! ^($ip4|$ip6) ($prefix)? !x; while (<DATA>) { next if $_ !~ $pattern; print "Hey, I found \$1: $1 \$2: $2\n"; } __DATA__ no match here 999.888.777.666/66 aaaa:aaaa:aaaa:aaaa:/77
      Labelling groups (with (?pattern) regex) showed me what was happening. It is all clear now.

      Many thanks to everyone, specially BillKSmith!

      I was having a trouble when using $1, $2... because I didn't know they exact behaviour.

      The problem I had was with the parenthesis inside $ip4 regex, causing $2 to have an ip address,
      $3 had the match inside the parenthesis in $ip4, and the prefix I expected put into $4:
      # Example of my debug output: line = '*> 177.101.16.0/21 200.19.74.230 0 200' matches: $1:'*> ' $2:'177.101.16.0' $3: '16.' $4:'/21'

      Now I understand how $1, $2, $3... are set.
      Thank you all, Monks! =D

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (15)
As of 2013-06-19 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many continents have you visited?









    Results (675 votes), past polls