MySQL正規表示式入門教程

2022-10-08 03:09:08 字數 3312 閱讀 9647

我們知道,在sql之中,可以用 like 這個謂詞(表示式) 來進行模糊檢索,並支援 %,?,_等佔位符.

但是,這個模糊檢索的功能有很多限制,簡單來說就是太模糊了。

在mysql中提供了 regexp 關鍵字來支援正規表示式,當然,只是一些很簡單的正則啦。

首先,我們構造一些測試資料。

複製** **如下:

-- 建表

use test;

drop table if exists t_regcustomer;

create table t_regcustomer (

id int(10) auto_increment

,name varchar(256)

,age int(10)

, primary key(id)

) collate='utf8_general_ci' engine=innodb;

增加一些測試資料:

複製** **如下:

-- 插入一些測試資料:

truncate table t_regcustomer;

insert into t_regcustomer(name, age) values ('王明',20);

insert into t_regcustomer(name, age) values ('王大',21);

insert intopcuybzw t_regcustomer(name, age) values ('小王',22);

insert into t_regcustomer(name, age) values ('小王2',22);

insert into t_regcustomer(name, age) values ('敲不死',23);

insert into t_regcustomer(name, age) values ('憨憨',24);

insert into t_regcustomer(name, age) values ('憨憨2',24);

insert into t_regcustomer(name, age) values ('郭靖名',25);

insert into t_regcustomer(name, age) values ('郭靖2',25);

insert into t_regcustomer(name, age) values ('郭靖3',25);

insert into t_regcustomer(name, age) values

('郭得缸',25)

,('大鵬',20)

,('大鵬2',20)

,('大鵬3',20)

,('二鵬',19)

,('鵬鵬',18)

,('鵬鵬1',18)

,('小鵬',17)

,('aaa',17)

,('aaa',17)

,('ss',17)

,('s2',17)

,('ss',17)

1. 最簡單的查詢:

複製** **如下:

select * 

from t_regcustomer; 

2. 指定列名查詢

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

;  3. 對查詢結果排序

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

order by c.age asc 

;  4. like 模糊檢索

%匹配任意數量(0~n)的任意字元

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

where c.name like '%鵬%' 

order by c.age asc 

;  5. regexp 關鍵字

.匹配任意乙個字元

注意此處因為沒有起始(^)和結束($)限定符,所以只要列**現的行都會被檢索出來.

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

where c.name regexp '.鵬.' 

order by c.age asc 

;  6. 正則起始限定符

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

where c.name regexp '^王' 

order by c.age asc 

;  7. 大小寫敏感

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

where c.name regexp binary '^s' 

order by c.age asc 

;  8. 正則或運算

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

where c.name regexp binary 'a|s'&nb程式設計客棧sp;

order by c.name asc 

;  9. 組運算正則

[123] 表示 1、2、3這3個數字之一出現即可

複製** **如下:

select c.id, c.name, c.age 

from t_regcustomer c 

where c.name regexp binary '鵬[123]' 

order by c.name asc 

;  [1-9] 匹pcuybzw配 1、2、3、.... 8、9

複製** **如下:

select c.id, c.name,程式設計客棧 c.age 

from t_regcustomer c 

where c.name regexp binary '鵬[1-9]' 

order by c.name asc 

;  10. 轉義

使用 \\

可以轉義 \.()?-| 以及分頁,換行符號等

11.更多內容

請查閱 《mysql必知必會》 68頁 正規表示式,pdf**位址:

本文標題: mysql正規表示式入門教程

本文位址:

正規表示式入門教程

元字元原義字元 非列印字元 字元類預定義類 邊界量詞 貪婪與懶惰 非貪婪 3.練習一下 先來看看幾個常用的案例。手機號碼正規表示式 1 345789 0 9 解釋 最外的 是正則的表示式的標誌,表示以什麼開頭,表示哪些可選項,表示出現幾次,以什麼結尾。手機號案例 以1開頭,第2位為 3,4,5,7,...

正規表示式入門教程

正規表示式,又稱正規表示法 常規表示法 英語 regular expression,在 中常簡寫為regex regexp或re 電腦科學的乙個概念。正規表示式使用單個字串來描述 匹配一系列符合某個句法規則的字串。在很多文字編輯器裡,正規表示式通常被用來檢索 替換那些符合某個模式的文字。在編寫處理字...

正規表示式入門教程

表1.常用的元字元 匹配除換行符以外的任意字元 w匹配字母或數字或下劃線或漢字 s匹配任意的空白符 d匹配數字 b匹配單詞的開始或結束 匹配字串的開始 匹配字串的結束 表2.常用的限定符 語法說明 重複零次或更多次 重複一次或更多次 重複零次或一次 重複n次 重複n次或更多次 重複n到m次 3 正規...