简体   繁体   中英

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?

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 (eg [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM