in reply to Re^2: Question on Regex grouping
in thread Question on Regex grouping

Numbering of captures counts per match/regex, of which you have two here - the one with the 8-digit capture being evaluated last.

If you wanted to extract the 5-digit number from the first regex, you could simply reverse the order of the &&-combined tests:

if (($arr =~ / (\w*)(def)(\d{8})/) && ($arr =~ /(\w*)(abc)(\d{5})/)) {

Now, $3 is the 5-digit number.

If you wanted to keep all captures, you could assign them to variables, e.g.:

if (( my ($c1, $c2, $c3) = $arr =~ /(\w*)(abc)(\d{5})/) && ( my ($c4, $c5, $c6) = $arr =~ / (\w*)(def)(\d{8})/)) {

Replies are listed 'Best First'.
Re^4: Question on Regex grouping
by ajguitarmaniac (Sexton) on Dec 21, 2010 at 07:05 UTC

    Great! Just what I was looking for! Can I use the below mentioned approach to arrive at the same result?

    if ($arr =~ /(\w*)(abc)(\d{5})/){ $a = $3 if (($arr =~ / (\w*)(def)(\d{8})/){ $b = $3 } } print $a; prnit $b;

      Yes.  But don't use $a and $b :)  — they're special global variables (for sort).

        Alright!! Thank you!! Let me see how sort works now :-)

      Also remember that if either (or both) or the regexes fail to match, one or the other (or both) of  $a and  $b (or whatever you finally decide to call them) will be undefined.