Solved

Pattern matching

Posted on 2000-02-29
7
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

749 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