hemc4
hemc4

Reputation: 1633

Go regex, Negative Look Ahead alternative

I am trying to implement the regex (?<!\\{)\\[[a-zA-Z0-9_]+\\](?!\\}) with go regex.

Match value will be like [ua] and [ua_enc] and unmatched should be {[ua]} and {[ua_enc]}

As Negative lookahead is not supported in Go, what may be the alternative expression for this?

Upvotes: 0

Views: 4990

Answers (1)

Amadan
Amadan

Reputation: 198436

There is no alternative expression for this. Using plain (?:[^{]|^)(...)(?:[^}]|$) to capture the intended match and assert the previous and next characters are not braces will kind-of work: you will need to work with the first capture group instead of with the full match, and it will fail when there is only a single character between two matches (e.g. [foo]_[bar]). The best way, really, is to use FindAllStringSubmatchIndex and manually check the previous and next characters to make sure they are not braces outside of regexp.

Upvotes: 2

Related Questions