in reply to Re: Regex help
in thread Regex help
gri6507, note that /$pattern+/ is exactly the same as /AB+/ (well, at least for $pattern = 'AB'), which is, by definition /A(:?B)+/.# 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;
I'm open for any suggestions, and yes, I do know Mastering Regular Expressions, I just forgot half (the important half) of it.$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"
|
|---|