I can check if a string equals to a given keyword with regular expressions. Here is an example:
Regex: /^hello$/
String: hello
Result: matches, as expected
Regex: /^goodbye$/
String: goodbye
Result: matches, as expected
Regex: /^bye$/
String: bye bye
Result: does not match, as expected
What I can’t achieve is to check if a string is not equal to a keyword. Here are some examples of what I am trying to do:
Regex: ^(?!hello).*$
String: bye
Result: matches, as expected
Regex: ^(?!hello).*$
String: bye bye
Result: matches, as expected
Regex: ^(?!hello).*$
String: say hello
Result: matches, as expected
Regex: ^(?!hello).*$
String: hello you
Result: does not match, but should match because "hello you" is not equal to "hello"
I think I am close with ^(?!hello).*$ but need a hand on this. Here is another example:
Regex: ^(?!fresh\sfruits).*$
String: fresh fruits
Result: does not match, as expected
Regex: ^(?!fresh\sfruits).*$
String: lots of fresh fruits
Result: matches, as expected
Regex: ^(?!fresh\sfruits).*$
String: fresh fruits, love them.
Result: does not match, but should match
Thanks!
Let’s just add the failing case to your expression:
So the first bit matches hello followed by anything save the empty string. (I just finished an automata class and can’t help but think of it as ε :P). The second bit matches anything not starting with
hello.That covers all the possible cases, I think.