iradik has asked for the wisdom of the Perl Monks concerning the following question:

suppose i have a string which looks like this:
(the . stands for a wildcard )
$string = a.foobara.foobara.foobara.bara.foobar x 100
i want to extract a.foobar from this string everytime it happens but i dont want the a.shoebar to be part of my extraction.
when i do:
@array = ($string =~ m/(a.*?foobar)/gso);
i match the string "a.bara.foobar". but i want a.bar (without a foo in front of bar) simply not to match! What is the proper re?

Replies are listed 'Best First'.
Re: repeating patterns
by Masem (Monsignor) on Mar 30, 2001 at 19:08 UTC
    Remember that '.' is a special regex character, and if you want to match it specifically, you need to escape it.
    @array = ($string =~ m/(a\.foobar)/gso);
    (Unless there's more to your question than it appears?)
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: repeating patterns
by Anonymous Monk on Mar 30, 2001 at 19:49 UTC
    let me clarify, the dot in the string just stands for a bunch of stuff which changes

      OK, you have a string that has an unknown number of occurences of the pattern "a (something)foobara"; and you want to grab all of those instances. The tag in the middle can change; so the key is to use the fact that you can interpolate variables into a regex.

      #since delimiter may contain special regex characters, # make sure you escape them my $delim = quotemeta("DELIMITER"); my @matches = $string =~ /a${delim}foobar/g;

      Will, *I think*, do what you want. But this might not get you what you want when you're dealing with real data, so beware.

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor