marscld has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, When using the reg-ex like following:  $_ =~ /([a-z]+(\d+))/

what does $1 and $2 refers to ? Could you please advise ?

Thanks for all the help.

Replies are listed 'Best First'.
Re: reg-ex pattern with nesting parethesis
by choroba (Cardinal) on Jun 11, 2014 at 09:21 UTC
    The rule is easy: However you nest the capture groups, the numbers correspond to the order of the opening parentheses.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: reg-ex pattern with nesting parethesis
by AppleFritter (Vicar) on Jun 11, 2014 at 09:13 UTC

    $1 and $2 refer to the first and second captures, respectively: in this case, $1 corresponds to the entire regex, while $2 refers to just the \d+.

    What they'll actually contain depends on what's in $_, of course.

      In this case, out most parenthesis would be matched first, then for nested ones. Thanks for clarification, AppleFritter.
Re: reg-ex pattern with nesting parethesis
by Corion (Patriarch) on Jun 11, 2014 at 09:16 UTC
Re: reg-ex pattern with nesting parethesis
by LanX (Saint) on Jun 11, 2014 at 09:41 UTC