简体   繁体   中英

Escaping question mark in mySQL regex

I have this simple query that uses regex. I don't know why it returns an error every time I try to run it.

select *
from mytable
where thumbnail_url regexp '^\?.+'

Basically, I want to see if any string in this list starts with a question mark or not. I am connected to Amazon RDS through MySQLWorkbench 6.3.

When you use a backslash to escape a regexp character in MySQL, you must also escape the backslash itself as \\\\ . MySQL would see a single \\ character and attempt to use it as an escaping character within the SQL statement itself rather than treating it as a literal. But you need it to act as a literal in order to modify the ? within the regex.

The first \\ escapes the second \\ , resulting in a literal regex sequence \\? to represent a single literal ? in the matching string.

MySQL documentation on string literals ...

Format your expression with an escaped backslash as:

select *
from mytable
where thumbnail_url regexp '^\\?.+'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM