Regular expression in MySQL to exclude what has a specific word inside parentheses
This is a regular expression for a MySQL query.
I want to exclude the following line because it has a something
parenthesis inside:
bla bla bla bla bla bla (bla bla bla something)
However, I want to include the following line because it doesn't have a something
parenthesis inside:
bla bla bla (bla bla bla)
I tried this query but it didn't work.
SELECT * FROM table WHERE field NOT REGEXP '((%something%))';
I think it's wrong. I was just doing trial and error. I like to use regex, but I never fully understand it. Are there any good tutorials / books / links for learning the details of regular expressions?
+2
a source to share
1 answer
Try:
SELECT * FROM table WHERE field NOT REGEXP '\\([^\\)]*something.*\\)'
Regular expression:
\([^\)]*something.*\)
(but MySQL treats \ as a special character, so we need to escape it as \).
It 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
+3
a source to share