Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matching multiple negative lookaheads

Tags:

I'm trying to match a string (using a Perl regex) only if it doesn't start with "abc:" or "defg:", but I can't seem to find out how. I've tried something like

^(?:(?!abc:)|(?!defg:)) 
like image 769
tobbo Avatar asked Nov 27 '14 23:11

tobbo


People also ask

What is Lookbehind in regex?

Lookbehind, which is used to match a phrase that is preceded by a user specified text. Positive lookbehind is syntaxed like (? <=a)something which can be used along with any regex parameter. The above phrase matches any "something" word that is preceded by an "a" word.

What is negative lookahead in regex?

Because the lookahead is negative, this means that the lookahead has successfully matched at the current position. At this point, the entire regex has matched, and q is returned as the match.

How do you use negative look ahead?

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 JavaScript regex support Lookbehind?

JavaScript doesn't support any lookbehind, but it can support lookaheads.


1 Answers

Lookahead (?=foo), (?!foo) and lookbehind (?<=foo), (?<!foo) do not consume any characters.

You can do multiple assertions:

^(?!abc:)(?!defg:) 

or:

^(?!defg:)(?!abc:) 

...and the order does not make a difference.

like image 126
Stephan Stamm Avatar answered Oct 21 '22 18:10

Stephan Stamm