If I have the input:
hello cat
hellocat
hello gat
I would like to find the a line that starts with the word "hello" and doesn't have the word "cat" after it.
Is it possible to negate a group, for example:
hello[^(\s?cat)]
Or are you only able to negate a set of characters in that position? If not, what are some ways to accomplish this? The only way that I've been able to do this is with a positive lookahead:
hello(?!\s?cat)
But I was wondering if there were alternative approaches to doing this.
There is also another way without look arounds which I think is worth mentioning as an interesting concept: /hello(?:\scat)|(hello\s.*)/
In this case we first match what we don't want (but don't capture it) then we only capture the second part if first part failed, which means that in the capture you will always have something that does not contain cat.
You can check in this example https://regex101.com/r/bydCGb/3, in the match information box, the "group 1" capture - and also check the substitution part - we never have the cat part.
According to your case, you can then say: if there are capturing group 1 then do something.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With