Go (RE2)

Negative Lookahead in GO

Go (RE2) can't run this pattern out of the box.

Try it in the GO tester →

Why it doesn't work in GO

Go's RE2 engine doesn't support lookarounds (`(?=...)`, `(?<=...)`, etc.) — they break the linear-time matching guarantee.

Approach

Restructure to capture the surrounding context as a group instead, or use JS / Python where lookarounds are available.

Workaround code in Go (RE2)

goGo (workaround)
package main

import (
	"fmt"
	"regexp"
	"unicode"
)

// RE2 doesn't support lookarounds. The fix: match a BROADER candidate
// without the lookaround, then verify the condition in Go code.
//
// Example: instead of `(?=.*\d)[A-Za-z\d]{8,}` (require a digit),
// match the candidate then check `HasDigit(s)` separately.

func HasDigit(s string) bool {
	for _, r := range s {
		if unicode.IsDigit(r) {
			return true
		}
	}
	return false
}

func main() {
	re := regexp.MustCompile(`[A-Za-z\d]{8,}`) // simplified, no lookaround
	input := "Password1 weakpass StrongerOne9"
	for _, candidate := range re.FindAllString(input, -1) {
		if HasDigit(candidate) { // the lookaround condition, in code
			fmt.Println(candidate)
		}
	}
}

Match a broader candidate without the lookaround, then verify the constraint in Go. Keeps the linear-time guarantee.

Pattern

regexGO
\b(?!error|warn|debug)[a-z]+\b   (flags: gi)

How the pattern works

\b is a word boundary. (?!error|warn|debug) is a zero-width assertion that fails if the upcoming text is one of the blocked words. [a-z]+ then matches an actual word. The result: every word EXCEPT error/warn/debug. Lookarounds work in JS and Python; Go's RE2 does NOT support them.

Examples

Input

info user login error timeout debug

Matches

  • info
  • user
  • login
  • timeout

Input

all words except WARN here

Matches

  • all
  • words
  • except
  • here

Input

error error error

No match

Same pattern, other engines

← Back to Negative Lookahead overview (all engines)