Solved

Pattern matching

Posted on 2000-02-29
7
161 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…

831 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