Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape square brackets in mysql REGEXP?

Tags:

regex

mysql

I have video embed code stored in a database table. We use multiple video sources, including YouTube, Viddler, and locally-stored flash files. I need to find all the records with flash files. The body field for a flash record looks like this:

[swf file="/sites/default/files/lecture-video/2010_02_beier_schanzer.swf" width="702" height="560"]

I was hoping to do something like this:

SELECT * FROM `node_revisions` inner join node on node_revisions.nid = node.nid where node.type = "video" and node_revisions.body REGEXP "^[swf"

but got the following error:

39 - Got error 'brackets ([ ]) not balanced' from regexp

How can I escape the bracket when it's the first character I'm looking for?

like image 458
EmmyS Avatar asked May 02 '11 16:05

EmmyS


People also ask

How do I remove square brackets in mysql?

You could also use trim() function to remove [] signs from customer column: SELECT c. *, cp. * FROM customer c LEFT JOIN customer_products cp ON trim(trailing ']' from trim(leading '[' from c.

How do I escape a square bracket in SQL?

To escape square brackets in LIKE you can use another square bracket to escape the original square bracket or use a custom escape character using the ESCAPE keyword in LIKE clause.

What is the difference between regexp and like in mysql?

Basically, LIKE does very simple wildcard matches, and REGEX is capable of very complicated wildcard matches. In fact, regular expressions ( REGEX ) are so capable that they are [1] a whole study in themselves [2] an easy way to introduce very subtle bugs.


1 Answers

You have to use two backslashes.

regexp '^\\[swf'
like image 103
Nicola Cossu Avatar answered Nov 01 '22 19:11

Nicola Cossu