In Perl 6 and .NET regexes, we can grab a variable number of captures in a single match, whereas in Perl 5 (and pretty much everywhere else) we cannot. Similarly, both Perl 6 and .NET allow variable length look-behinds in regexes, whereas Perl 5 (and pretty much everyone else) does not.
As brian d foy says here, we can use a negative look-behind in Perl 5 to find occurrences of bar that are not immediately preceded by foo like so
'foobar' =~ /(?<!foo)bar/
but we cannot check for a variable number of o's with o+
'foobar' =~ /(?<!fo+)bar/
In Perl 5, this gives
Variable length lookbehind not implemented in regex m/(?<!fo+)bar/
I guess there is some hope that it will be implemented one day. I mean, that's a step up from a plain old error, right?
Anyway, in PowerShell (which uses .NET regexes), we can use exactly the regex above
PS> 'foobar' -match "(?<!fo+)bar" False
In Rakudo (which uses Perl 6 rules), there is new syntax for it
> 'foobar' ~~ /<!after fo+>bar/ #<failed match>
Nice! Neither the variable number of captures nor the variable length negative look-behind features is important enough to me to get me to switch from Perl 5 to either PowerShell or Perl 6 anytime soon, but they sure look nice to have!
Jeff "japhy" Pinyan discovered a work-around at least ten years ago: sexeger. You can turn any look-behind into a look-ahead by reversing both string and pattern. Variable-length look-ahead is implemented.
Posted by: Anonymous | 12/03/2011 at 04:58 PM
So we just need the ~= operator to do this for us automatically! :)
Posted by: oylenshpeegul | 12/04/2011 at 09:09 AM