undefinedfix
Sign in

MYSQL LIKE

kp97338 edited in Mon, 01 Aug 2022
Problem description
SELECT 'b' REGEXP 'b'; 匹配成功; SQL中b就是一个普通的字符b;正则中匹配的是一个字符b;

SELECT 'b' REGEXP '\b'; 匹配失败;  SQL中b就是一个普通的字符b;正则中匹配的内容是 一个字符\ 和 字符b;

SELECT '\b' REGEXP '\b'; 匹配成功 ;  SQL中\b 代表一个 A backspace character ; 正则中的\b 匹配的也是 A backspace character;

SELECT '\b' REGEXP '\\b'; 匹配失败 ;  SQL中\b 代表一个 A backspace character ; 正则中的\\b 匹配一个字符b; \\在正则中是转义符号;

SELECT '\b' REGEXP '\\\b'; 匹配成功; SQL中\b 代表一个 A backspace character ; 正则中的\\\也把\转义为普通的字符\; 随后字符\ 和字符b结合组成 \b;
\b 又代表 A backspace character ;
Now I want to make sure that the content above is right, especially the understanding of each example

Here are some other things to ignore
SELECT 'a\b' LIKE 'a\b'; 执行成功
SELECT 'a\b' REGEXP 'a.b'; 匹配失败, . 可以匹配除了 \ 意外的任何其他字符
SELECT '2.3' REGEXP '[0-9]\\.[0-9]';  使用\\ 进行转义
2 Replies
Mahdi
commented on Mon, 01 Aug 2022
SELECT "a\b" LIKE '%\b'; 

SELECT "a\\b" LIKE '%b';

https://dev.mysql.com/doc/ref...


Escape Sequence    Character Represented by Sequence
\0    An ASCII NUL (X'00') character
\'    A single quote (') character
\"    A double quote (") character
\b    A backspace character
\n    A newline (linefeed) character
\r    A carriage return character
\t    A tab character
\Z    ASCII 26 (Control+Z); see note following the table
\\    A backslash (\) character
\%    A % character; see note following the table
\_    A _ character; see note following the table
Chad
commented on Mon, 01 Aug 2022

This is about the escape. However, I haven't encountered writing SQL by myself. Thank you for reminding me.