关键字regexp用来表示后面跟的东西作为正则表达式处理。
(.)是正则表达式的一个符号,表示匹配任意一个字符:
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)
|匹配符:
表示匹配其中之一
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)
[]匹配符: 匹配几个字符之一
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)
匹配特殊字符,必须用//为前导。
mysql> select prod_name from products where prod_name regexp '//.' ;+--------------+| prod_name|+--------------+| .5 ton anvil |+--------------+1 row in set (0.00 sec)
匹配字符类:
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)
定位符用法:
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)
以上都是mysql正则表达式的用法。
