Solved

Pattern matching

Posted on 2000-02-29
7
157 Views
Last Modified: 2012-05-04

Hi,

 I have a variable like this,
$var_name = "AA first BB second AA third BB fourth AA 5th BB";
I want to read the characters between 'AA' and 'BB' ie., (first, third, 5th).

[The number of AA-BB combinations is not defined.]

How do I do this?
0
Comment
Question by:leoleo
7 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2571735
$var_name = "AA first BB second AA third BB fourth AA 5th BB";
@Bits = split /AA|BB/, $var_name;
foreach $Chunk (@Bits) {
   print "$Chunk\n";
}
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2571744
$var_name = "AA first BB second AA third BB fourth AA 5th BB";
@Bits = split /AA|BB/, $var_name;
foreach $Chunk (@Bits) {
   print "$Chunk\n";
}
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2571747
Hey, it got posted twice (and its not perfect).

I have now reread the question, and here is the right answer:

$var_name = "AA first BB second AA third BB fourth AA 5th BB";
@Bits = split /BB/, $var_name;
foreach $Chunk (@Bits) {
   ($Bad, $Good) = split /AA/, $Chunk;
   print "$Good\n";
}
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Accepted Solution

by:
mrachow earned 50 total points
ID: 2571839
Try minimal matching (achieved by this extra '?':
@arrMic = $var_name =~ /AA(.*?)BB/g;
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2571865
Cunning, mrachow
0
 

Author Comment

by:leoleo
ID: 2571877

Thanks, PC_User321 & mrachow. Both the solns worked.
0
 
LVL 3

Expert Comment

by:alien_life_form
ID: 2572000
Greetings.

@ the very least, I would use /\bAA\b/ and /\bBB\b/ as splitters. These will work assuming your fields do not contain AA or BB isolated by non word characters.

The proposed solution does not work on, for instance:

AA ABBA BB ...


OTOH, if a separator can be a valid field value, no amount of regexp will do, and you have to resort to more detailed parsing.

Cheers,
  alf

 
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

19 Experts available now in Live!

Get 1:1 Help Now