在SQL中使用正則表達式

  1. 在字符串的開始處進行匹配:mysql

mysql> select 'wqh' regexp '^w';正則表達式

+-------------------+sql

| 'wqh' regexp '^w' |ide

+-------------------+regexp

|                 1 |ci

+-------------------+字符串

1 row in set (0.00 sec)it


mysql> class

2.在字符串的末尾處進行匹配:email

mysql> select 'wqh' regexp 'h$';

+-------------------+

| 'wqh' regexp 'h$' |

+-------------------+

|                 1 |

+-------------------+

1 row in set (0.00 sec)


mysql> 


3.匹配任意單個字符,包括換行符:

mysql> select 'abcd' regexp '.c', 'abcd' regexp '.f';    

+--------------------+--------------------+

| 'abcd' regexp '.c' | 'abcd' regexp '.f' |

+--------------------+--------------------+

|                  1 |                  0 |

+--------------------+--------------------+

1 row in set (0.00 sec)


mysql> 

4.匹配括號內的任意字符:

mysql> select 'abcdefh' regexp '[fhk]';

+--------------------------+

| 'abcdefh' regexp '[fhk]' |

+--------------------------+

|                        1 |

+--------------------------+

1 row in set (0.00 sec)


mysql>


實例:

mysql> select first_name,email from customer where email regexp "@163[,.]com$";  

+------------+---------------+

| first_name | email         |

+------------+---------------+

| 11         | bj@163.com    |

| 11         | bsssj@163.com |

+------------+---------------+

2 rows in set (0.00 sec)

mysql> select first_name,email from customer where email regexp ".*@163[,.]com$"; 

+------------+---------------+

| first_name | email         |

+------------+---------------+

| 11         | bj@163.com    |

| 11         | bsssj@163.com |

+------------+---------------+

2 rows in set (0.00 sec)


mysql> select first_name,email from customer where email regexp ".*@163.com$";   

+------------+---------------+

| first_name | email         |

+------------+---------------+

| 11         | bj@163.com    |

| 11         | bsssj@163.com |

+------------+---------------+

2 rows in set (0.00 sec)


mysql> select first_name,email from customer where email regexp "@163.com$";  

+------------+---------------+

| first_name | email         |

+------------+---------------+

| 11         | bj@163.com    |

| 11         | bsssj@163.com |

+------------+---------------+

2 rows in set (0.00 sec)


mysql> 



若是不實用正則表達式:

mysql>  select first_name,email from customer where email like "%@163%.com" or email like "%@163%,com" ;

+------------+---------------+

| first_name | email         |

+------------+---------------+

| 11         | bj@163.com    |

| 11         | bsssj@163.com |

+------------+---------------+

2 rows in set (0.00 sec)


mysql> 

相關文章
相關標籤/搜索