Jack Holmes
Jack Holmes

Reputation: 23

Go Regex negative lookbehind

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?

Upvotes: 2

Views: 802

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627145

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.

Upvotes: 0

Related Questions