Mysql learning-mysql is required. 1 is required for mysql. Chapter 9 is required:
Regular expressions are used to match special character sets. Mysql uses the where clause to provide preliminary support for regular expressions.
The keyword regexp is used to indicate the things following it as a regular expression.
(.) Is a regular expression symbol that matches any character:
mysql> select prod_name-> from products-> where prod_name regexp '.000'-> order by prod_name;+--------------+| prod_name|+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.14 sec)
| Match character:
Indicates matching one of them
mysql> select prod_name-> from products-> where prod_name REGEXP '1000|2000'-> ORDER BY prod_name;+--------------+| prod_name|+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.00 sec)
[] Match character: match one of several characters
2 rows in set (0.00 sec)mysql> select prod_name-> from products-> where prod_name regexp '[123] Ton'-> ;+-------------+| prod_name |+-------------+| 1 ton anvil || 2 ton anvil |+-------------+2 rows in set (0.00 sec)
mysql> select prod_name from products where prod_name regexp '[1-5] Ton';+--------------+| prod_name|+--------------+| .5 ton anvil || 1 ton anvil|| 2 ton anvil|+--------------+3 rows in set (0.02 sec)
mysql> select prod_name from products where prod_name regexp '[^1-3] Ton';+--------------+| prod_name|+--------------+| .5 ton anvil |+--------------+1 row in set (0.00 sec)
Special characters must be matched. // must be used as the leading character.
mysql> select prod_name from products where prod_name regexp '//.' ;+--------------+| prod_name|+--------------+| .5 ton anvil |+--------------+1 row in set (0.00 sec)
Matching character class:
mysql> select prod_name from products where prod_name REGEXP '//([0-9] sticks?//)' order by prod_name;+----------------+| prod_name|+----------------+| TNT (1 stick)|| TNT (5 sticks) |+----------------+2 rows in set (0.05 sec)mysql> select prod_name from products where prod_name REGEXP '[[:digit:]]{4}' order by prod_name;+--------------+| prod_name|+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.00 sec)
Positioning operator usage:
mysql> select prod_name-> from products-> where prod_name REGEXP '^[0-9//.]'-> order by prod_name;+--------------+| prod_name|+--------------+| .5 ton anvil || 1 ton anvil|| 2 ton anvil|+--------------+3 rows in set (0.00 sec)
These are the usage of mysql regular expressions.