Bug #101186 Regexp does not handle square brackets and hyphen in proper way
Submitted: 15 Oct 2020 12:35 Modified: 15 Oct 2020 17:42
Reporter: Kamil Holubicki (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:5.6, 5.7 OS:Any
Assigned to: CPU Architecture:Any

[15 Oct 2020 12:35] Kamil Holubicki
Description:
Hyphens and square brackets are not handled in a proper way.

How to repeat:
Hyphen:

mysql> select 'abc-def/_1' regexp '^[[:alnum:]:\\/\\-_]+$';
+----------------------------------------------+
| 'abc-def/_1' regexp '^[[:alnum:]:\\/\\-_]+$' |
+----------------------------------------------+
|                                            0 |
+----------------------------------------------+
1 row in set (0.00 sec)

However:

mysql> select 'abc-def/_1' regexp '^[[:alnum:]:\\/_\\-]+$';
+----------------------------------------------+
| 'abc-def/_1' regexp '^[[:alnum:]:\\/_\\-]+$' |
+----------------------------------------------+
|                                            1 |
+----------------------------------------------+
1 row in set (0.00 sec)

Square brackets:
mysql> select '[abc]' regexp '^[[:alnum:]\\[\\]]+$';
+---------------------------------------+
| '[abc]' regexp '^[[:alnum:]\\[\\]]+$' |
+---------------------------------------+
|                                     0 |
+---------------------------------------+
1 row in set (0.00 sec)

However:

mysql> select '[abc' regexp '^[[:alnum:]\\[]+$';
+-----------------------------------+
| '[abc' regexp '^[[:alnum:]\\[]+$' |
+-----------------------------------+
|                                 1 |
+-----------------------------------+
1 row in set (0.00 sec)
[15 Oct 2020 17:32] Kamil Holubicki
mysql> select 'abc-def/_1' regexp '^[[:alnum:]:\\-/_]+$';
ERROR 1139 (42000): Got error 'invalid character range' from regexp
[15 Oct 2020 17:42] MySQL Verification Team
Thank you for the bug report.

c:\dbs>c:\dbs\8.0\bin\mysql -uroot --port=3580 -p --prompt="mysql 8.0 > "
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 Source distribution BUILT: 2020-10-03 06:40:05

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql 8.0 > select 'abc-def/_1' regexp '^[[:alnum:]:\\-/_]+$';
+--------------------------------------------+
| 'abc-def/_1' regexp '^[[:alnum:]:\\-/_]+$' |
+--------------------------------------------+
|                                          1 |
+--------------------------------------------+
1 row in set (0.02 sec)
========================================================================

c:\dbs>57c

c:\dbs>c:\dbs\5.7\bin\mysql -uroot --port=3570 -p  --prompt="mysql 5.7 > " --default-character-set=latin1
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 Source distribution BUILT: 2020-10-03 06:01:18

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql 5.7 > select 'abc-def/_1' regexp '^[[:alnum:]:\\-/_]+$';
ERROR 1139 (42000): Got error 'invalid character range' from regexp
mysql 5.7 >

========================================================================

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.50 Source distribution BUILT: 2020-10-03 05:39:51

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql 5.6 > select 'abc-def/_1' regexp '^[[:alnum:]:\\-/_]+$';
ERROR 1139 (42000): Got error 'invalid character range' from regexp
mysql 5.6 >