There are many ways to go about this.

First, do not put parens '()' around things that you have no interest in using later. "Capturing" these things consumes time and resources and to no effect.

In general, I avoid using $1, $5 etc. Use Perl list slice instead. Assign directly to a variable like the code below shows. As you write more and more Perl code this $1, $2 stuff will appear less and less often.

This term m/abc\d{5}/ is a pre-condition - I have no problem at all with writing code that says: "forget this line if that pre-condition is not satisfied" and that is what the code below does.

Trying to compress things into a single statement gives Perl a bad name as a "write only" language and that reputation is undeserved! I am a big fan of both C and of Perl. It is easy to write obscure stuff in both languages, but you don't have to!

#!/usr/bin/perl -w use strict; my @x = ( 'smomedef12345', 'anabc12345 and there is some def12345678', 'qwerabc12345def55', 'def87654321abc54321', ); foreach (@x) { next unless (m/abc\d{5}/); #pre-condition to look further (my $string8) = m/def(\d{8})/; #puts $string8 in list context #$string8 = (m/def(\d{8})/)[0]; #alternate way with list slice if ( !defined($string8) ) { $string8 = 'undefined'; #Perl 5.10 has a special way to do this #Probably here just do "next;" # because an undefined value means the # regex above did not match! } print "var def=$string8\n"; } __END__ prints: ..note that first item is silently skipped! var def=12345678 var def=undefined var def=87654321 #note that this works even though #the pre-condition of abc\d{5} #occurs later in the line! Wow!
Update: ok a more obtuse solution:
my @x = ( 'smomedef12345', 'anabc12345 and there is some def12345678', 'qwerabc12345def55', 'def87654321abc54321', ); print map{ /abc\d{5}/ and /def(\d{8})/ ? "def=$1\n" : () }@x; __END__ prints: def=12345678 def=87654321
Does essentially the same thing but in a much more obtuse way.
I think the first code is better for a lot of reasons.

In reply to Re: Question on Regex grouping by Marshall
in thread Question on Regex grouping by ajguitarmaniac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.