Solved

simple regexp...

Posted on 2000-05-09
3
216 Views
Last Modified: 2010-03-05
How can I get the 5th and 6th letters from an 8 letter code and assign them to a scalar variable?

For example, if the code is ABCDEFGH I'd like the scaler to have the value of EF.
thanks,
0
Comment
Question by:frenomulax
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
jyu_88 earned 50 total points
ID: 2795373
you don't need a reg. substr function will take care of it.

my $code="ABCDEFGH";
my $fifthAndSixth = substr($code, 4, 2);

substr(string, index of char to start, how many char in total including the starting char);

index of char in a string starts with 0, thus the '4' is the above snippet for the fifth char in the string. 'man perlfunc' for more details.

If you insist to use reg, you can use something like,

my $code="ABCDEFGH";
my $fifthAndSixth;
if ($code =~ /^\w{4}(\w{2})\w{2}$/) {
     $fifthAndSixth = $1
}
else {
     print "invalid code: <$code>\n";
}
0
 

Author Comment

by:frenomulax
ID: 2795426
Just what I need. Thanks!
0
 

Author Comment

by:frenomulax
ID: 2795427
Just what I need. Thanks!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question