Solved

simple regexp...

Posted on 2000-05-09
3
181 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
Comment Utility
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
Comment Utility
Just what I need. Thanks!
0
 

Author Comment

by:frenomulax
Comment Utility
Just what I need. Thanks!!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

12 Experts available now in Live!

Get 1:1 Help Now