in reply to Match and extract three characters from the middle of a string

You were close :)

First, your code won't compile, unless you've accidentally transposed some characters in typing it in over here.

Second,

This might do what you want:

$inq = '23D78000'; if( $inq =~ m/^[0-9]{2}([A-Z][0-9]{2})[0-9]+$/ ){ $emc_device = $1; }

Alternatively, if you really like the style you're using above,

$inq = '23D78000'; ( $emc_device ) = $inq =~ m/^[0-9]{2}([A-Z][0-9]{2})[0-9]+$/;

Note that unless you want to keep $junk and $junk1, you don't need to capture them with parens. But if you DO want to keep them, this will work:

$inq = '23D78000'; ( $junk, $emc_device, $junk1 ) = $inq =~ m/^([0-9]{2})([A-Z][0-9]{2})( +[0-9]+)$/;

Also, if the number of digits in front of your emc_device changes, you'll need to slightly adjust your regex, or perhaps make it more flexible.



--chargrill
$,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}