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[$%++]}

In reply to Re: Match and extract three characters from the middle of a string by chargrill
in thread Match and extract three characters from the middle of a string by svnipp

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.