Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a regex lookahead/lookbehind in mysql

I'm trying to do something like

SELECT * FROM table WHERE column REGEXP (abc)(?=def)

and I got the error

Got error 'repetition-operator operand invalid' from regexp

due to the '?' -> see #1139 - Got error 'repetition-operator operand invalid' from regexp

Is there an equivalent in mysql that I don't see in https://dev.mysql.com/doc/refman/5.7/en/regexp.html ?

or maybe another mysql function that I don't know yet?

like image 229
guillaume latour Avatar asked Sep 27 '16 14:09

guillaume latour


1 Answers

MySQL REGEXP does not support lookaheads, but you can try to achieve the same logic using something like this:

WHERE column LIKE 'abc%' AND
      SUBSTRING(column, INSTR(column, 'abc') + 3, 3) <> 'def'
like image 127
Tim Biegeleisen Avatar answered Sep 26 '22 10:09

Tim Biegeleisen