Q:

Extracting specific values from a Perl regex

I want to use a Perl regex to extract certain values from file names.
They have the following (valid) names:
testImrrFoo_Bar001_off
testImrrFooBar_bar000_m030
testImrrFooBar_bar231_p030

From the above I would like to extract the first 3 digits (always guaranteed to be 3), and the last part of the string, after the last _ (which is either off, or (m orp) followed by 3 digits). So the first thing I would be extracting are 3 digits, the second a string.
And I came out with the following method (I realise this might be not the most optimal/nicest one):
my $marker = '^testImrr[a-zA-z_]+\d{3}_(off|(m|p)\d{3})$';
if ($str =~ m/$marker/)
{
    print "1=$1 2=$2";
}

Where only $1 has a valid result (namely the last bit of info I want), but $2 turns out empty. Any ideas on how to get those 3 digits in the middle?

A:

You were almost there.
Just :
 - capture the three digits by adding parenthesis around: (\d{3})
 - don't capture m|p by adding ?: after the parenthesis before it ((?:m|p)), or by using [mp] instead:  
^testImrr[a-zA-z_]+(\d{3})_(off|[mp]\d{3})$

And you'll get :
1=001 2=off
1=000 2=m030
1=231 2=p030

