Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative lookahead in Text.Regex.Posix

Tags:

regex

haskell

I am trying to use negative lookahead – standart regex-posix practice:

"foobarbaz" =~ "^(?!.*bar).*$" :: Bool

and getting error *** Exception: user error (Text.Regex.Posix.String died: (ReturnCode 13,"repetition-operator operand invalid"))

How can I use negative lookahead in Haskell?

like image 684
Andrey Kuznetsov Avatar asked Apr 15 '14 07:04

Andrey Kuznetsov


People also ask

What is negative lookahead in regex?

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. Positive lookahead works just the same.

What is ?: In regex?

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.

Can I use negative lookahead?

Negative lookahead That's a number \d+ , NOT followed by € . For that, a negative lookahead can be applied. The syntax is: X(?! Y) , it means "search X , but only if not followed by Y ".

Does grep support negative lookahead?

Negative lookahead, which is what you're after, requires a more powerful tool than the standard grep . You need a PCRE-enabled grep. If you have GNU grep , the current version supports options -P or --perl-regexp and you can then use the regex you wanted.


1 Answers

Disclaimer: there are too many GREP dialects!

Your original GREP works on InDesign (it uses a slightly amended boost implementation); so does this alternative:

^((?!bar).)*$

i.e., try to match (?!bar). on each character in turn.

like image 87
Jongware Avatar answered Oct 24 '22 05:10

Jongware