Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl re negative look behind Variable length error

Tags:

regex

perl

This regular expression works fine for matching the pattern 'ab_' when not preceded by a single quote or a dollar sign:

/(?<!('|\$))ab_/

but if I try, for example, to add a bracket before the single quote

/(?<!(\['|\$))ab_/

I get this error

Variable length lookbehind not implemented in regex;

What does this error mean and is there a way to make the second example work? It is likely I am overlooking something basic since I am no expert, so please point out anything I am missing.

like image 743
BryanK Avatar asked Mar 09 '14 05:03

BryanK


People also ask

How to do negative lookahead regex?

Negative lookahead provides the solution: q(?! u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead, we have the trivial regex u.

What is negative look behind?

A negative lookbehind assertion asserts true if the pattern inside the lookbehind is not matched.

What is positive lookbehind?

In positive lookbehind the regex engine searches for an element ( character, characters or a group) just before the item matched. In case it finds that specific element before the match it declares a successful match otherwise it declares it a failure.

What is regex lookahead?

Lookahead is used as an assertion in Python regular expressions to determine success or failure whether the pattern is ahead i.e to the right of the parser's current position. They don't match anything. Hence, they are called as zero-width assertions.


1 Answers

The error means that in Perl, a lookbehind assertion has to have a fixed-length pattern. ('|\$) is fine because the pattern only matches a length-1 substring, but (\['|\$) could match either a length-1 substring ($) or a length-2 substring ([').

In your case, you can fix this by just using two separate lookbehinds, each with a fixed-length pattern, one for each case you want to rule out:

/(?<!\[')(?<!\$)ab_/
like image 115
ruakh Avatar answered Sep 25 '22 08:09

ruakh