日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Mysql与正则表达式笔记-松勤

發布時間:2024/3/24 数据库 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql与正则表达式笔记-松勤 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

正則表達式是用來匹配文本的特殊的字符集合,將一個正則表達式與文本串進行比較,Mysql中用where子句提供支持,正則表達式關鍵字:regexp

1、使用‘|’匹配兩個串中的一個

2、使用‘[]’匹配幾個字符中的一個

3、使用‘[^]’不匹配幾個字符串中的任意一個,表示否定

4、對于特殊字符的匹配
需要在待匹配的特殊字符前面加上‘\’進行轉義。

5、Mysql支持的正則表達式字符集

正則表達式是用來匹配文本的特殊的字符集合,如果想從一個文本文件中提取電話號碼,可以用正則表達式完成

作用:匹配文本,將一個正則表達式與一個文本串進行比較
格式:where子句中使用regexp 關鍵字,后面接正則表達式字符

與Like的區別:
where可以使用like (not like)和regexp (not regexp)來匹配特定的內容
1、like匹配整列數據
2、regexp 可以匹配列中任意位置的文本,更靈活更強大

完全匹配
select prod_name from products where prod_name regexp ‘1000’;

等同于like語句
select prod_name from products where prod_name like ‘%1000%’;

| 兩個串中間的一個,效果等同于or or是完全匹配,不能模糊匹配

或者,不能放在like后面,放在regexp后面
select prod_name from products where prod_name regexp ‘1000|2000’;

[] 匹配同個字符中的一個,與in相似,in是完全匹配

匹配1或2或3
select prod_name from products where prod_name regexp ‘[123] ton’;
匹配包含12 ton數據
select prod_name from products where prod_name regexp ‘[123][123] ton’;

[^ ] 不匹配幾個字符中的任意一個

不匹配括號中任意一個字符的結果,除了123,其它都會被查詢出來
select prod_name from products where prod_name regexp ‘[^123] ton’;

[1-9]匹配一個范圍1到9的數字
[a-z]包含a到z的任意一個字符

匹配1至5的任意數字 1 ton ,2 ton,3 ton,4 ton,5 ton
select prod_name from products where prod_name regexp ‘[1-5] ton’;

\對于特殊字符的匹配需要轉義

匹配點.
select vend_name from vendors where vend_name regexp ‘\.’;

正則表達式字符集

[:alnum:]任意字母和數字[a-zA-Z0-9]
[:alpha:]任意字符[a-zA-Z]
[:upper:]任意大寫字母[A-Z]
[:lower:]任意小寫字母[a-z]
[:digit:]任意數字[0-9]
[:xdigit]任意十六進制字[a-fA-F0-9]
[:blank:]空格和制表[\t]
[:space:]包括空格在內的任意空白字符[\f\n\r\t\v]
[cntrl:] ascii控制字符0-37和127
[:graph:]與[:print:]相同,不包括空格
[:print:]任意可打印字符
[:punct:]排除[:alnum]和[:cntrl:]的任意字符

  • 0或多個匹配 與Like后面的%相似
  • 1個或多個匹配{1,} 針對這個位置前面的字符匹配
    ? 0個或1個匹配 {0,1}
    n 指定數目的匹配
    {n,} 不少于指定數目的匹配 出現n次以上
    {n,m} 匹配數目的范圍,m不超過255 出現n次以上少于m次
    ^ 文本的開始
    $ 文本的結尾
    [[:<:]] 單詞的開始[[:<:]] a app
    [[:>:]] 單詞的結尾

匹配連在一起的4位數字
select prod_name from products where prod_name regexp ‘[[:digit:]]{4}’;

練習

查找prod_id包含anv的數據
select * from products where prod_id regexp ‘anv’;

如果需要區分大小寫,binary關鍵字
select * from products where binary prod_id regexp ‘ANV’;

查找prod_id包含anv或者包含f的數據
select * from products where prod_id regexp ‘anv|f’;

包含3,c,e的數據
select * from products where prod_id regexp ‘[3ce]’;

select * from products where prod_id regexp ‘[^3ce]’; 沒有定位哪一個字符

查找f開頭不包含3,c,e的數據
select * from products where prod_id regexp ‘f[^3ce]’;

查找prod_desc含有.開頭的數據
select * from products where prod_desc regexp ‘^\.’;

前面是任意字符與數字,點后面有9或沒有都可以被查找出來
select * from products where prod_price regexp ‘[[:alnum:]]\.9*’;

9后面出現一個或多個都能被查出來
select * from products where prod_price regexp ‘[[:alnum:]]\.9+’;

點后面出0個或1個都可以被查詢出來
select * from products where prod_price regexp ‘[[:alnum:]]\.9?’;

?是針對.的限制,有沒有.都可以被查出來
select * from products where prod_price regexp ‘[[:alnum:]]\.?’;

查找以k結尾的數據
select * from products where prod_price regexp ‘k$’;

查找d開頭的數據
select * from products where prod_price regexp ‘^d’;

查找單個d的數據
select * from products where prod_price regexp ‘^d$’;

查找以d開頭以d結尾的數據
select * from products where prod_price regexp ‘^d.*d$’;

查找以p單詞開頭的數據
select * from products where prod_desc regexp '[[:<:]]'p;

查找以f結尾單詞的數據
select * from products where prod_desc regexp ‘f[[>:]]’;

更多學習資料,可以私信我獲取

總結

以上是生活随笔為你收集整理的Mysql与正则表达式笔记-松勤的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。