With REGEXP, you need a double backslash to match a metacharacter literally:
mysql>
mysql> CREATE TABLE mytable
-> (
-> c CHAR(1)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO mytable (c)
-> VALUES
-> ('%'),
-> ('_'),
-> ('.'),
-> ('^'),
-> ('$'),
-> ('\\')
-> ;
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT * FROM mytable;
+------+
| c |
+------+
| % |
| _ |
| . |
| ^ |
| $ |
| \ |
+------+
6 rows in set (0.00 sec)
mysql>
mysql> SELECT c, c REGEXP '\\.', c REGEXP '\\^', c REGEXP '\\$' FROM mytable;
+------+----------------+----------------+----------------+
| c | c REGEXP '\\.' | c REGEXP '\\^' | c REGEXP '\\$' |
+------+----------------+----------------+----------------+
| % | 0 | 0 | 0 |
| _ | 0 | 0 | 0 |
| . | 1 | 0 | 0 |
| ^ | 0 | 1 | 0 |
| $ | 0 | 0 | 1 |
| \ | 0 | 0 | 0 |
+------+----------------+----------------+----------------+
6 rows in set (0.00 sec)
mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)
Related examples in the same category