Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use negative lookbehind with quantifiers?

Tags:

regex

perl

I want to use negative lookbehind with quantifiers. But I have the error. I use Perl.

I have several text files and I need to detect that they have special text prefix before the distinct word.

Error:

A quantifier inside a lookbehind makes it non-fixed width

My regular expression:

/(?<!big([\s]+)?)dog/

I want to get the fourth and fifth number.

1. bigdog - not ok
2. big   dog - not ok
3. big dog - not ok
4. dog - ok
5. dog any text except big.  dog - ok
like image 812
Roman Domoratskii Avatar asked Dec 14 '18 14:12

Roman Domoratskii


1 Answers

You can use the SKIP and FAIL verbs.

#! /usr/bin/perl
use warnings;
use strict;

use Test::More tests => 7;

my $regex = qr/big\s*dog(*SKIP)(?!)|dog/;

unlike 'bigdog',    $regex;
unlike 'big dog',   $regex;
unlike 'big   dog', $regex;
unlike 'cat',       $regex;

like 'dog',       $regex;
like 'small dog', $regex;
like 'medium dog and big dog', $regex;

qr/(?<!big)\s*dog/ is problematic as

big dog
    ^
    |
matches here: is not preceded by "big" but "big "
like image 80
choroba Avatar answered Sep 22 '22 02:09

choroba