Solved

simple regexp...

Posted on 2000-05-09
3
194 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 172
stftime format 4 55
Using Perl to parse rows 7 93
XML::LibXML and Xpath syntax How do I get attribute of sibling 2 117
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now