Solved

Pattern matching

Posted on 2000-02-29
7
159 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
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.

 
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

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

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

23 Experts available now in Live!

Get 1:1 Help Now