I need to write a regular expression that will match everything in the string unless it has a certain word in it. Taking this string for example:
http://12.34.567.890/sqlbuddy
The expression that matches everything is:
^.*$
...Which needs to be modified so that it will not match the string at all if it contains the word "sqlbuddy". I thought that a negative lookahead would do it, but that's not working for me.
For example, I tried this, which doesn't work:
^(?!sqlbuddy).*$
How should I modify this?
The example doesn't work because it assumes sqlbuddy
is in the beginning of the string. You need to change it to the following, so that sqlbuddy
can appear anywhere in the string:
^(?!.*sqlbuddy.*).*$
However, if you really just want to check if your string contains a given word, then maybe a "http://12.34.567.890/sqlbuddy".contains("sqlbuddy")
will suffice.
Also, this worked for me: Position of the string 'sqlbuddy' doesn't matter.
^((?!sqlbuddy).)*$
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