tags:

views:

37

answers:

1

This Regular expression if for MySQL query:

I want to exclude this row because it has 'something' in side the bracket "bla bla bla bla bla bla (bla bla bla something)"

However I want to include this 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 regular expression, but never understand it completely. is there any good tutorial/books/links for learning the detail of regular expression?

Thank You

+1  A: 

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
psmears
actually the answer before you was right '\(.*something.*\)'but he deleted it
bn
This one is actually slightly better, because `(.*something.*)` will also match `(otherstuff) something (otherstuff)` i.e. it can match even if the 'something' isn't inside `()`.(It's possible to fool this one too, but only if you allow multiple levels of `()`).
psmears
yes im using that one, I feel bad that I said it didnt work. but I like your explainations to your answer. thanks!
bn