当前位置:网站首页>Mysql語句的RegExp的用法與案例
Mysql語句的RegExp的用法與案例
2022-07-22 14:40:00 【QTWWlhh】
Mysql語句的RegExp的用法與案例
一、RegExp基本語法
1. JavaScript RegExp 參考手册
2. MySQL REGEXP:正則錶達式查詢
3. MySQL中REGEXP正則錶達式使用大全
字符 | 描述 |
---|---|
^ | 從開始行開始匹配 |
$ | 從末端行開始匹配 |
. | 匹配任意單個字符除了換行符 |
[ ] | [ ] 內為字符集,匹配 [ ] 內任一字符 |
[^ ] | 匹配除了方括號裏的任意字符 |
* | 匹配零個或多個在它前面的字 符 |
+ | 匹配前面的字符 1 次或多次 |
? | 匹配 0 或 1 個 ?號前的字符。 |
{n,m} | 匹配前面的字符串至少 n 次, 至多 m 次 |
(xyz) | 匹配與 xyz 完全相等的字符串 |
\s | 匹配任意單個字符除了換行符 |
\ | 轉義字符,匹配一些保留的字符 [ ] ( ) { } . * + ? ^ $ \ |
[.characters.] 在括號錶達式中(使用[和]),匹配用於校對元素的字符序列,字符為單個字符或新行等字符名
mysql> SELECT ‘~’ REGEXP ‘[[.~.]]’; -> 1
mysql> SELECT ‘~’ REGEXP ‘[[.tilde.]]’; -> 1
[=character_class=]
在括號錶達式中(使用[和]),[=character_class=]錶示等同類。它與具有相同校對值的所有字符匹配,包括它本身,
[[=a=]] 等同於[a(+)],[a+],[a{
1,}]
[:character_class:]
在括號錶達式中(使用[和]),[:character_class:]錶示與術語類的所有字符匹配的字符類。
標准的類名稱是:
alnum 文字數字字符
alpha 文字字符
blank 空白字符
cntrl 控制字符
digit 數字字符
graph 圖形字符
lower 小寫文字字符
print 圖形或空格字符
punct 標點字符
space 空格、制錶符、新行、和回車
upper 大寫文字字符
xdigit 十六進制數字字符
[[:<:]], [[:>:]]
這些標記錶示word邊界,它們分別與word的開始和結束匹配
word是一系列字字符,其前面和後面均沒有字字符。字符是alnum類中的字母數字字符或下劃線(_)
二、Mysql使用RegExp案例
案例1:在json數據裏面,找到class_detail_students_id這個字段,並且數據包含學生編號“58888828”
思路:
在這個數據 -> {
"class_id":2,"class_detail_students_id":",58888881,58888828,58888881,"}
需要查找 -> class_detail_students_id 這個字段
並查看 -> ,58888881,58888828,58888881, 裏面是否包含58888828
注意點:數據裏面有雙引號,這是regexp數據可以用單引號包圍,裏面的雙引號用\\轉義
匹配規則regexp ==>'(\\"class_detail_students_id\\")(:\\")[(0-9)|,]*(,58888828,)[(0-9)|,]*(\\")'
解答完整的sql:
select * from mole_class_detail_change where
current_value regexp '(\\"class_detail_students_id\\")(:\\")[(0-9)|,]*(,58888828,)[(0-9)|,]*(\\")'
案例2:匹配000000002 _ _ _ _ _ _ _ _ 1900 和 000000003 _ _ _ _ _ _ _ _1700,其中"__" 這是8個空比特,只能是數字
思路:
這是兩個字符串,都空出中間8為數字,可以用"()"
(000000002) -> 匹配 "000000002"
[[:digit:]] -> 匹配 數字字符
{
8} -> 匹配前一個括號裏面8次
| -> 或的意思
解答完整的sql:
select * from mole_class_detail_change
where class_detail_code regexp '((000000002)([[:digit:]]{8})(1900))|((000000003)([[:digit:]]{8})(1700))'
案例3:每個用戶有多個身份權限,查詢用戶同時有a、b 、 c 權限的用戶
上方截圖展示了錶數據 和分組查詢的結果
. -> 匹配任意單個字符除了換行符
* -> 匹配零個或多個在它前面的字符
+ -> 匹配1個或多個在它前面的字符
[abc] -> 匹配a/b/c裏面的一個字符
[,|,.+,] -> 匹配','(逗號) 或者 ',abcfdg等等,'(逗號包圍的數據)
匹配規則regexp ==> '.*,[abc][,|(,.+,)][abc][,|(,.+,)][abc][,|(,.+,)]'
解答完整的sql:
select user_id,concat(',',GROUP_CONCAT(DISTINCT role_id),',') as group_role from test GROUP BY user_id
having group_role regexp '.*,[abc][,|(,.+,)][abc][,|(,.+,)][abc][,|(,.+,)]'
边栏推荐
- Special analysis of China's third-party payment market in 2022
- Mysql索引分類及其使用實例
- 【多线程】并行和并发的区别
- Web Automation - selenium
- The overheated weather in the UK led to the interruption of Google cloud and Oracle cloud services
- 【Cicadaplayer】Release无法断点调试的问题
- rexroth比例阀4WRPEH6C3B04L-3X/M/24F1-885
- PyTorch模型训练:设置随机种子,保证每次训练的结果都一样
- Swoole-TCP服务器
- 2022 crane driver (bridge crane only) examination question bank and answers
猜你喜欢
MySql集群之主从复制(一)
Parker hydraulic oil pump pv140r1k1t1nmm1
Classification de l'index MySQL et exemples d'utilisation
Parker hydraulic oil pump pvp3336r2m
Binary tree recursive advanced [next]
[pingtunnel tool tutorial] ICMP encapsulation and firewall penetration using ICMP tunnel technology
OS7-资源的死锁
Cluster configuration and verification of MySQL Cluster (4)
Daily question brushing record (30)
2022r1 fast opening pressure vessel operation test and simulation test
随机推荐
Time complexity and complexity
【大型电商项目开发】线程池-异步(CompletableFuture)-48
微波雷达传感器模块,智能感知人体存在,实时感应交互控制应用
OS09- AutoSAR OS事件机制和实例解析
深度学习-神经网络-循环神经网络(一):RNN(Recurrent Neural Network,循环神经网络;1990年)
Parker hydraulic oil pump pvp3336r2m
2022 melting welding and thermal cutting operation certificate examination question bank simulated examination platform operation
2022r1 fast opening pressure vessel operation test and simulation test
MySQL index classification and its application examples
行业分析| 物流对讲
2022 crane driver (bridge crane only) examination question bank and answers
慧通编程第5关 - 魔法学院第7课
MFC obtains cfindreplacedialog control messages by registering window information
要想上云之旅中不被“甩掉”,云服务供应商的企业生命力是关键
2022r1 fast opening pressure vessel operation test and simulation test
idm下载器是什么软件?最新V6.41版本号Win下载工具
Programming example of STM32 state machine -- fully automatic washing machine (Part 1)
网络分析---并查集上做差分
DCM09- ReadDataByIdentifier ($22)的功能和配置【基于DaVinci Configurator Classic】
SOC first project