Regular expression in MySQL to exclude something that has specific word inside brackets

mysql, regex

Solution

Try:

SELECT * FROM table WHERE field NOT REGEXP '\\([^\\)]*something.*\\)'

The regexp is:

\([^\)]*something.*\)

(but MySQL treats \ as a special character so we have to escape it as \).

That means:

\(         - an open-parentheses character ("(" has a special meaning
             in regular expressions, so we have to escape it with "\")
[^\)]      - any character except a ")"...
*          - ... repeated any number of times
something  - the string to match
.          - any character
*          - ... repeated any number of times
\)         - a ")" character

Problem

This regular expression is for a MySQL query. I want to exclude the following row because it has `something` inside the bracket: ``` bla bla bla bla bla bla (bla bla bla something) ``` However I want to include the following row, because it does not have `something` inside the bracket: ``` bla bla bla (bla bla bla) ``` I tried this query but it didnt work. ``` SELECT * FROM table WHERE field NOT REGEXP '((%something%))'; ``` I think this is wrong. I just did trial and error. I like to use a regular expression, but I never understand it completely. Are there any good tutorials/books/links for learning the detail of regular expressions?

Original source