svnipp has asked for the wisdom of the Perl Monks concerning the following question:

This is a very simple problem, and I feel pretty stupid asking. I have tried working thru this by reading the Regular Expression tutorial Perl doc, but I'm missing something here...

What I need to to match three characters from the middle of a string. For instance, the string 23D78000, I need to simply extract the D78 to input into a database table. My only stumbling point is the actual regexp match. Here is the latest of several things I have tried. Please don't hurt yourselves laughing...

$inq = "23D78000"; ($junk, $emc_device, $junk1) = ($inq =~ /(\w\w)(\w\w\w*$/);

Code tags and formatting added by GrandFather

2006-07-22 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Simple regex matching problem...'

Replies are listed 'Best First'.
Re: Match and extract three characters from the middle of a string
by ikegami (Patriarch) on Jul 21, 2006 at 19:48 UTC
    my $emc_device = substr($inq, 2, 3);
    which is the faster equivalent of
    my ($emc_device) = $inq =~ /^..(...)/;
Re: Match and extract three characters from the middle of a string
by chargrill (Parson) on Jul 21, 2006 at 19:49 UTC

    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[$%++]}
Re: Match and extract three characters from the middle of a string
by neilwatson (Priest) on Jul 21, 2006 at 19:41 UTC
Re: Match and extract three characters from the middle of a string
by planetscape (Chancellor) on Jul 22, 2006 at 02:19 UTC
Re: Match and extract three characters from the middle of a string
by perlsen (Chaplain) on Jul 22, 2006 at 03:30 UTC
    Hi svnipp,
    If you want to extract D78 like format text you may try this,
    $inq = "23D78000"; ($junk, $emc_device, $junk1) = ($inq =~ /^(.*)(\w{3})(\w{3})$/); print "Result: [$junk], [$emc_device], [$junk1]"; #Result: [23], [D78], [000]
    Regards,
    perlsen
Re: Match and extract three characters from the middle of a string
by rsriram (Hermit) on Jul 22, 2006 at 06:14 UTC

    Hi, You can also use substr to extract a part of the stirng.

    $inq="23D78000";
    print substr($inq,2,3);

    prints the output you need. substr can be used to extract parts of variable. It takes 3 parameters. First is the variable you need to work on, second is the number of characters that needs to be stripped out from left and third is the number of characters that needs to be retained in the string. Hope this helps!!! For further information, see manpage for substr()

Re: Match and extract three characters from the middle of a string
by sh1tn (Priest) on Jul 22, 2006 at 10:34 UTC
    Just count carefully:
    # . (dot) symbol means any character # take 3rd, 4th and 5th chars $inq =~ m|..(...)|; print $1;


Re: Match and extract three characters from the middle of a string
by Leviathan (Scribe) on Jul 22, 2006 at 13:26 UTC

    And if the string is variable in length,

    my $emc_device = substr($inq, (length($inq) - 3)/2, 3);
    --
    Leviathan