Go Regex negative lookbehind

Viewed 133

I have a regex that works well for PCRE to detect a certain word without any special characters ahead of it. For example detecting T4 would work well with (?!\S)T4(?!\S).

Match: T4

Not a match: ^T4 or =T4

However, I'm not certain if negative look behinds are supported by Go Regex. Is there something equivalent that can be used in Go Regex?

1 Answers

Neither negative lookbehinds nor lookarounds in general are supported by Golang regex.

You can check if there is a word in between whitespaces or start/end of string using

pattern := regexp.MustCompile(`(?:\s|^)T4(?:\s|$)`)

where

  • (?:\s|^) - a non-capturing group matching either a whitespace or start of string
  • T4 - a literal substring
  • (?:\s|$) - a non-capturing group matching either a whitespace or end of string.
Related