I am trying to not match negated word in a sentence such as adjective with not, never
for example. Currently I used character level negation. Example for not
^(?:[^n]+|n(?:$|[^o]|o(?:$|[^t]|\w)))
. Is there an easy way (more readable) of doing this for one or many negation words (not, never, any, nobody, ......
). Here is a code for not
negation:
package main
import(
"fmt"
"regexp"
)
func main (){
sentence:="he is not satisfied"
re := regexp.MustCompile(`^(?:[^n]+|n(?:$|[^o]|o(?:$|[^t])))\ssatisfied`)
fmt.Println(re.FindAllString(sentence, -1))
sentence="he is satisfied"
fmt.Println(re.FindAllString(sentence, -1))
}
Thanks
Unfortunately, for performance reasons (though the Go regexp parser is not particularly performant anyway), Go's regexp package does not support lookahead/lookbehind, so there is no great way to do this using regular expressions.
Regular expressions aren't a great fit for this anyway, though. It requires a very complex expression to do a very simple match. If I were implementing something similar, I would probably just split the string on spaces/punctuation and do my own matching. Easier to work with and likely better performance.