0

I inherited some perl 5 (5.20) code, which contains a regular expression with the operator ?>!. Unfortunately, I have been unable to find any documentation on what this operator is doing at all, in man perlre only ?> is documented.

Can anyone explain to me when the regex in the example code matches/doesn't match?

my $text = "powergenerator";
if ($text =~ m/generator(?>!power)/) {
    print "MATCH\n";
} else {
    print "NO MATCH\n";
}

There is a comment in the original code that the intended use case is that generator is matched when it is not the context of powergenerator, but, according to my tests, this is not what happens. Note: The code only cares about whether the regex matches or not, it does not process the match location.

2 Answers2

2

There is no such operator. (?>!power) is the (?>...) (non-backtracking) construct, matching the literal string !power.

It was probably intended to be (?<!power) (negative lookbehind), judging from the example... but that won't work either if the intention is to get a "NO MATCH" — it simply asserts that the string "rator" doesn't equal "power", which is always true. The correct way to do what seems to be wanted depends on exactly what is wanted, but this isn't it.

hobbs
  • 223,387
  • 19
  • 210
  • 288
1

Likely this is a typo of (?!. As is it is (?> followed by the literal string !power.

ysth
  • 96,171
  • 6
  • 121
  • 214