in reply to Regex help

Not really sure what the point of this is, but if you add some more parens you should get what you want:
my ($start,$middle,$end) = $string =~ /^(.*?)(($pattern)+)(.*?)$/g;
By placing the + outside of the first "parened" $pattern, you allow more than one - then, put some parens around that to catpure the results to $2.

Hope this helps, :)

UPDATE:
Oops, almost got that right ... now we are trying to match 4 items, not 3 anymore ... so try this:
my ($start,$middle,undef,$end) = $string =~ ...
UPDATE 2:
I like liz's and CombatSquirrel's suggestion to use a look ahead non-capturing paren group ... but none of these solutions (yes bart, including mine ;)) are robust. For example, none will work with the pattern BABABABBB ...

UPDATE 3:
split! of course! i like it!

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: Regex help
by CombatSquirrel (Hermit) on Aug 24, 2003 at 15:39 UTC
    Errm, jeffa, that woud screw the my ($start,$middle,$end) part up.
    Suggested alternatives:
    # first one my ($start, $middle, undef, $end) = $string =~ /^(.*?)(($pattern)+)(.* +?)$/g; # second one, with non-capturing parens -- I like it better my ($start,$middle,$end) = $string =~ /^(.*?)((?:$pattern)+)(.*?)$/g;
    gri6507, note that /$pattern+/ is exactly the same as /AB+/ (well, at least for $pattern = 'AB'), which is, by definition /A(:?B)+/.
    Hope this helped.
    CombatSquirrel.

    Update: Arghh - wrong order for capturing and non-capturing parens. Fixed.

    Update 2: jeffa is right. The following RegEx should do the trick:
    $pattern = 'AB'; 'BABABABBB' =~ / ^ # start at beginning of line ( # capture to $1 .*? # a number of character, but as few as possi +ble ... (?<!$pattern) # ... which may not contain $pattern ) ( # capture to $2 (?:$pattern)+ # multiple occurences of $pattern | # OR (?!.*?$pattern) # nothing, BUT there may be no $pattern in t +he rest # of the string ) (.*) # capture rest to $3 /x ; print "$1<$2>$3$/"; __END__ prints "B<ABABAB>BB"
    I'm open for any suggestions, and yes, I do know Mastering Regular Expressions, I just forgot half (the important half) of it.

    Update 3 (Explanation): The RegEx engine tries to match at the earliest possible position. Therefore it will always match nothing to be captured in $1 (non-greedy dot-star), the highest possible number of following pattern matches (greedy star) and then the rest. Meaning, if the first pattern does not begin at the first character, $2 will also be empty (after all a star does not have to match) and the rest is slurped into $3. Bon appetit!
Re: Re: Regex help
by BrowserUk (Patriarch) on Aug 24, 2003 at 15:44 UTC

    You can avoid the extraneous capture by using non-capturing parens.

    my( $start, $middle, $end ) = $string =~ m[^ (.*?) ( (?:AB)+ ) (.*?) $ +]xg

    ...but you know that:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Re: Regex help
by gri6507 (Deacon) on Aug 24, 2003 at 15:35 UTC
    Thank you, that does exactly what I need.