当前位置:网站首页>MySQL regexp: Regular Expression Query
MySQL regexp: Regular Expression Query
2022-07-05 01:26:00 【Know its black and accept its white】
Read the directory
- This paper
- REGEXP Matching lists commonly used in operators
- Query records starting with a specific character or string
- Query records ending with a specific character or string
- Use symbols “.” Replace any character in the string
- Use “*” and “+” To match multiple characters
- Matches the specified string
- Match any one of the specified strings
- Matches characters other than the specified characters
- Application example 1
This paper
MySQL Formal expressions in are often used to retrieve or replace text content that conforms to a pattern , Match the special string that meets the requirements in the text according to the specified matching pattern .
for example , Extract the phone number from a file , Find the repeated words in an article or replace the sensitive vocabulary entered by the user , Regular expressions can be used in these places .
Regular expressions are powerful and flexible , Commonly used for complex queries .
REGEXP Matching lists commonly used in operators
MySQL Use in REGEXP Keyword specifies the character matching pattern of the regular expression , The following table lists them REGEXP Matching lists commonly used in operators .
| Options | explain | Example | Examples of matching values |
|---|---|---|---|
| ^ | Match the start character of the text | '^b' Match with letters b start String | book、big、banana、 bike |
| $ | Match the end character of the text | 'st$’ Match with st Ending word Fu string | test、resist、persist |
| . | Match any single character | 'b.t’ Match any b and t There is a character between | bit、bat、but、bite |
| * | Match zero or more words in front of it operator | 'f*n’ Matching character n There is Arbitrary characters f | fn、fan、faan、abcn |
| + | Match preceding characters 1 Times or times | 'ba+’ Match with b start , after At least one face follows a | ba、bay、bare、battle |
| < character string > | Matches text containing the specified character | 'fa’ | fan、afa、faad |
| [ Character set ] | Match any word in the character set operator | '[xz]' matching x perhaps z | dizzy、zebra、x-ray、 extra |
| [^] | Match any characters that are not in brackets | '[^abc]’ Match any non package contain a、b or c String | desk、fox、f8ke |
| character string {n,} | Match the previous string at least n Time | b{2} matching 2 One or more Of b | bbb、 bbbb、 bbbbbbb |
| character string {n,m} | Match the previous string at least n Time , at most m Time | b{2,4} Match the least 2 individual , most 4 individual b | bbb、 bbbb |
Query records starting with a specific character or string
character "^" Matches text that begins with a specific character or string .
【 example 1】
stay tb_departments In the table , Inquire about dept_name Fields are represented by letters “C” The first record , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP '^C';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 1 | Computer | 11111 | A |
| 3 | Chinese | 33333 | B |
+---------+-----------+-----------+-----------+
2 rows in set (0.05 sec)
stay tb_departments There are two records in the table dept_name Field values are in letters C At the beginning , The returned results are 2 Bar record .
【 example 2】
stay tb_departments In the table , Inquire about dept_name Field to “Ch” The first record , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP '^Ch';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 3 | Chinese | 33333 | B |
+---------+-----------+-----------+-----------+
1 row in set (0.03 sec)
Only Chinese In order to “Ch” At the beginning , Therefore, only 1 Bar record .
Query records ending with a specific character or string
character “$” Matches text that ends with a specific character or string .
【 example 3】
stay tb_departments In the table , Inquire about dept_name Fields are represented by letters “y” The record at the end , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP 'y$';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 4 | Economy | 44444 | B |
| 5 | History | 55555 | B |
+---------+-----------+-----------+-----------+
2 rows in set (0.00 sec)
stay tb_departments There are two records in the table dept_name Field values are in letters y At the end of the , The returned results are 2 Bar record .
【 example 4】
stay tb_departments In the table , Inquire about dept_name Field to “my” The record at the end , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP 'my$';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 4 | Economy | 44444 | B |
+---------+-----------+-----------+-----------+
1 row in set (0.00 sec)
Only Economy In order to “my” At the end of the , Therefore, only 1 Bar record .
Use symbols “.” Replace any character in the string
【 example 5】
stay tb_departments In the table , Inquire about dept_name The field value contains the letters “o” And letters “y”, And there is only one letter between two letters , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP 'o.y';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 4 | Economy | 44444 | B |
| 5 | History | 55555 | B |
+---------+-----------+-----------+-----------+
2 rows in set (0.00 sec)
In the query statement “o.y” Specify that there should be letters in the matching characters o and y, And there is a single character between the two letters , There is no limit to the position of the matching character and the total length of the query string , therefore Economy and History All meet the matching conditions .
Use “*” and “+” To match multiple characters
asterisk “*” Match the preceding character any number of times , Include 0 Time . plus “+” Match the preceding character at least once .
【 example 6】
stay tb_departments In the table , Inquire about dept_name The field value contains the letters “C”, And “C” The letters appear after them “h” The record of , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP '^Ch*';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 1 | Computer | 11111 | A |
| 3 | Chinese | 33333 | B |
+---------+-----------+-----------+-----------+
2 rows in set (0.00 sec)
asterisk “*” Can match any number of characters ,Computer Middle letter C There is no letter after it h, But it also meets the matching conditions .
【 example 7】
stay tb_departments In the table , Inquire about dept_name The field value contains the letters “C”, And “C” The letters appear after them “h” At least one record , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP '^Ch+';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 3 | Chinese | 33333 | B |
+---------+-----------+-----------+-----------+
1 row in set (0.00 sec)
“h+” Match the letter “h” At least once , Only Chinese Meet the matching conditions .
Matches the specified string
A regular expression can match a specified string , As long as this string is in the query text , To match multiple strings , Then a separator is used between multiple strings “|” separate .
【 example 8】
stay tb_departments In the table , Inquire about dept_name The field value contains a string “in” The record of , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP 'in';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 3 | Chinese | 33333 | B |
+---------+-----------+-----------+-----------+
1 row in set (0.00 sec)
You can see ,dept_name Field Chinese Contains the string “in”, Meet the matching conditions .
【 example 9】
stay tb_departments In the table , Inquire about dept_name The field value contains a string “in” perhaps “on” The record of , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP 'in|on';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 3 | Chinese | 33333 | B |
| 4 | Economy | 44444 | B |
+---------+-----------+-----------+-----------+
2 rows in set (0.00 sec)
You can see ,dept_name Field Chinese Contains the string “in”,Economy Contains the string “on”, Meet the matching conditions .
Tips :LIKE Operator can also match the specified string , But with REGEXP Different ,LIKE If the matching string appears in the middle of the text , Can't find it , The corresponding line will not return . and REGEXP Match within text , If the matched string appears in the text ,REGEXP It will be found , The corresponding line will also be returned .
Match any one of the specified strings
square brackets “[]” Specify a character set , Match only any of these characters , This is the text you are looking for .
【 example 10】
stay tb_departments In the table , Inquire about dept_name The field value contains the letters “o” perhaps “e” The record of , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP '[io]';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 1 | Computer | 11111 | A |
| 3 | Chinese | 33333 | B |
| 4 | Economy | 44444 | B |
| 5 | History | 55555 | B |
+---------+-----------+-----------+-----------+
4 rows in set (0.00 sec)
You can see from the query results that , Of all returned records dept_name The value of the field contains letters o perhaps e, Or both .
square brackets “[]” You can also specify a set of values .
【 example 11】
stay tb_departments In the table , Inquire about dept_call The field value contains 1、2 perhaps 3 The record of , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_call REGEXP '[123]';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 1 | Computer | 11111 | A |
| 2 | Math | 22222 | A |
| 3 | Chinese | 33333 | B |
+---------+-----------+-----------+-----------+
3 rows in set (0.00 sec)
In the query results ,dept_call There is 1、2、3 One of the three numbers is the matching record field .
Match set “[123]” Or you could write it as “[1-3]”, That is, specify the set interval . for example ,“[a-z]” Indicates that the set interval is a~z The letter of ,“[0-9]” Indicates that the set interval is all numbers .
Matches characters other than the specified characters
“[^ Character set ]” Matches any character that is not in the specified set .
【 example 12】
stay tb_departments In the table , Inquire about dept_name The field value contains the letters a~t Records of characters other than , Input SQL The statement and execution results are as follows .
mysql> SELECT * FROM tb_departments
-> WHERE dept_name REGEXP '[^a-t]';
+---------+-----------+-----------+-----------+
| dept_id | dept_name | dept_call | dept_type |
+---------+-----------+-----------+-----------+
| 1 | Computer | 11111 | A |
| 4 | Economy | 44444 | B |
| 5 | History | 55555 | B |
+---------+-----------+-----------+-----------+
3 rows in set (0.00 sec)
Returns... In the record dept_name The field value contains values other than the specified letters and numbers , Such as u、y etc. , These letters are not in a~t in , Meet the matching conditions .
Application example 1
Scenario introduction :
Precise query question_id contain 2 Of Record

Use regular
SELECT * FROM yexam_exam where question_ids REGEXP '(^|,)2(,|$)';
Query results :
边栏推荐
- 【LeetCode】88. Merge two ordered arrays
- Four pits in reentrantlock!
- La jeunesse sans rancune de Xi Murong
- Global and Chinese markets for stratospheric UAV payloads 2022-2028: Research Report on technology, participants, trends, market size and share
- If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
- Wechat applet: new independent backstage Yuelao office one yuan dating blind box
- LeetCode周赛 + AcWing周赛(T4/T3)分析对比
- User login function: simple but difficult
- The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
- Blue Bridge Cup Square filling (DFS backtracking)
猜你喜欢

【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
![[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #](/img/1c/deaf20d46e172af4d5e11c28c254cf.jpg)
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #
![[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40](/img/e4/0bdee782a65028b2bc87db85d48738.png)
[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40
![抓包整理外篇——————状态栏[ 四]](/img/88/8267ab92177788ac17ab665a90b781.png)
抓包整理外篇——————状态栏[ 四]

【CTF】AWDP总结(Web)

BGP comprehensive experiment

Remote control service

微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益

Expose testing outsourcing companies. You may have heard such a voice about outsourcing

ROS command line tool
随机推荐
实战模拟│JWT 登录认证
PHP 约瑟夫环问题
视频网站手绘
Async/await you can use it, but do you know how to deal with errors?
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
【海浪建模2】三维海浪建模以及海浪发电机建模matlab仿真
Global and Chinese markets of emergency rescue vessels (errv) 2022-2028: Research Report on technology, participants, trends, market size and share
Kibana installation and configuration
Discrete mathematics: Main Normal Form (main disjunctive normal form, main conjunctive normal form)
Pandora IOT development board learning (RT thread) - Experiment 4 buzzer + motor experiment [key external interrupt] (learning notes)
Are you still writing the TS type code
SAP ui5 application development tutorial 106 - how to improve the readability of SAP ui5 application routing URL trial version
142. Circular linked list II
Database postragesq BSD authentication
A simple SSO unified login design
Database postragesql lock management
Digital DP template
La jeunesse sans rancune de Xi Murong
【FPGA教程案例10】基于Verilog的复数乘法器设计与实现
There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have