in reply to Re: Regex help
in thread Regex help

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!