Is there any workaround for lookbehind(negative lookahead) in re2j?

5,754 views
Skip to first unread message

Arushi Rai

unread,
Dec 2, 2016, 2:37:46 PM12/2/16
to re2j-discuss
I want to add negative lookahead in my regex and so far I have not found any solution? 

(?!) is not supported in re2j  

here's an example of what i am trying to do,

(?!bar|foo|\d)\b(http:\/\/google.com)

so basically I want http://google.com to not start with bar, foo or any digit

Alan Donovan

unread,
Dec 2, 2016, 2:55:13 PM12/2/16
to Arushi Rai, re2j-discuss
By design, RE2 doesn't support lookahead, positive or negative, since it would requires an algorithm with worst-case exponential running time.  RE2's algorithm runs in time proportional to the length of the input string.

You can find out more by starting here: https://swtch.com/~rsc/regexp/regexp3.html

Arushi Rai

unread,
Dec 2, 2016, 3:12:23 PM12/2/16
to re2j-discuss, arushi...@gmail.com
Thank you for your response. 

I understand this. However, I wanted to know if there is any workaround to achieve what I explained in the example? 

Alan Donovan

unread,
Dec 2, 2016, 3:21:16 PM12/2/16
to Arushi Rai, re2j-discuss
On 2 December 2016 at 15:12, Arushi Rai <arushi...@gmail.com> wrote:
Thank you for your response. 

I understand this. However, I wanted to know if there is any workaround to achieve what I explained in the example? 

You can't do it in a single regexp.  You could use two matchers, one looking for just the target string ("http://google.com") and one looking for the target string preceded by the unwanted stuff.  Then, use program logic to filter out the matches of the first that are also matches of the second.  (The first matcher is so simple you needn't use a regexp.)

Ivan Blanco

unread,
Apr 5, 2021, 1:17:55 PM4/5/21
to re2j-discuss
Hello there, I have made it using two regexs within AND: one contains the things you are looking, and the other has another regex looking for the things you do not want to be after you were looking before. I've used the NOT in the second REGEX:
For example:
=AND(REGEXMATCH(B2;"\bAs\b");NOT(REGEXMATCH(B2;"\bAs (if|success|failure|long|such|\w+ as)\b")))
It looks for any text containing As that is not followed by  if or success or failure or long or such or any word plus as
I think this way is much better than normal negative lookahead.

El dia divendres, 2 de desembre de 2016 a les 21:21:16 UTC+1, adonovan va escriure:
Reply all
Reply to author
Forward
0 new messages