当前位置:网站首页>Regular expressions in MySQL
Regular expressions in MySQL
2022-07-02 07:28:00 【Snow wind in the night sky】
Preface
Sometimes use MySQL When querying data in the database ,like Query has limitations , It can be used at this time MySQL The way of regular expression query in .
Regular expressions are special strings used to match text ( Character set ), Put a pattern ( Regular expressions ) Compare with a text string .
- Extract phone numbers from text files
- Find a file with a number in the middle of its name
- Repeated words in the text block
- Replace page URL For these URL Actual link to ( One or two regular expressions )
LIKE and REGEXP The difference between
LIKE Match the entire column (like It's fuzzy query ), If the matched text appears only in the column value ,LIKE Will not find it , The corresponding line will not return ( Of course , Use wildcards except )
REGEXP Match within column values , If the matched text appears in the column value ,REGEXP It will be found , The corresponding line is returned , At this time, a very important difference ( Of course , If you adapt to positioning symbols ^ and $, Can achieve REGEXP Match the entire column instead of a subset of the column )
for example :
Query a table name The column contains chen String data , Two article sql The statement is as follows
select * from stu_info where name like ' The small white ';
select * from stu_info where name regexp ' The small white ';
The first query result is name The value is equal to ’ The small white ‘ The data of , The second query result is name Contained in the ’ The small white ’ The data of , It can also be used. like( Fuzzy query + wildcard ) To achieve regexp Query results .
OR Match
select column from table where column regexp 'X|Y' order by column;
select column from table where column regexp '[XY]test' order by column;
The first way of writing will be | Apply to the whole string , As long as there is X or Y The data of , Will be retrieved
The second way of writing only [XY] Apply to the beginning of the search pattern
Match range
([]) A set can be used to define one or more characters to match , such as [0123456789], To simplify this type of collection , You can use (-) To define a range , namely [0-9],[a-z]
Match special character
Then the expression is composed of special characters with specific meanings ; If you want to match these special characters , It needs to be used. \ As the leader , This treatment is called escape (escaping), All characters with special meaning in regular expressions must be escaped in this way ;( To match the backslash {} Character itself , Need to use \\)
Be careful : Most regular expression implementations use a single backslash to escape special characters , So that these characters themselves can be used , and MySQL Two backslashes are required (MySQL Explain one for yourself , The regular expression library interprets a )
Match character class
Sometimes we need to retrieve the numbers we need 、 Matching of all alphabetic characters or all alphanumeric characters, etc , We can use predefined character sets , Called character class .
Match multiple instances
Sometimes it is necessary to have more control over the number of matches , such as : Find all the numbers , No matter how many numbers are included in the number , Or find a word and follow it s( If there is ) , etc. , We can use repeated metacharacters in regular expressions to complete .
select column from table where column regexp '\\([0-9] sticks?\\)' order by column;
The above regular expression ‘\([0-9] sticks?\)’ Meaning for :\ matching ([0-9] Match any number , This refers to the range of matching values ,sticks? matching stick and sticks{? send s Optional , because ? Match any character before it 0 perhaps 1 time }).
Locator
Sometimes in order to match the text in a specific position , Need to use locator , The list of commonly used locators is as follows :
Find one by one ( Including the number starting from the decimal point ) All values at the beginning , Simple search [0-9\.] or ([[:digit:]\.]) no way , Because it will find a match anywhere in the text , have access to ^ Locator , as follows :
select column from table where column regexp '^[0-9\\.]' order by column;
^ There are two uses
- In the assembly ( use [ and ] Definition ), Use it to negate the set
- Used of the beginning of a string
send regexp and like Do the same thing :like Match whole string two regexp Match the strings , You can use locators , use ^ Start each expression , use $ End each expression , You can make regexp and like It works the same .
Inquire about admin In the table group Field , The data format is :1,2,3;4,5,6;1,87,101
For example, query 101:
select * from xin_admin where `group` REGEXP '[,|;]?101[,|;]?';
边栏推荐
- 【Torch】解决tensor参数有梯度,weight不更新的若干思路
- Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix
- ORACLE EBS中消息队列fnd_msg_pub、fnd_message在PL/SQL中的应用
- 传统目标检测笔记1__ Viola Jones
- Alpha Beta Pruning in Adversarial Search
- 一份Slide两张表格带你快速了解目标检测
- Practice and thinking of offline data warehouse and Bi development
- Sparksql data skew
- Yaml file of ingress controller 0.47.0
- 使用Matlab实现:弦截法、二分法、CG法,求零点、解方程
猜你喜欢
oracle apex ajax process + dy 校验
Classloader and parental delegation mechanism
ORACLE EBS ADI 开发步骤
[paper introduction] r-drop: regulated dropout for neural networks
腾讯机试题
SSM实验室设备管理
[introduction to information retrieval] Chapter 6 term weight and vector space model
常见CNN网络创新点
sparksql数据倾斜那些事儿
[introduction to information retrieval] Chapter 3 fault tolerant retrieval
随机推荐
oracle-外币记账时总账余额表gl_balance变化(上)
Drawing mechanism of view (3)
Oracle rman半自动恢复脚本-restore阶段
Illustration of etcd access in kubernetes
[tricks] whiteningbert: an easy unsupervised sentence embedding approach
深度学习分类优化实战
Oracle段顾问、怎么处理行链接行迁移、降低高水位
Implementation of purchase, sales and inventory system with ssm+mysql
SSM实验室设备管理
Error in running test pyspark in idea2020
Oracle 11g sysaux table space full processing and the difference between move and shrink
SSM student achievement information management system
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
Classloader and parental delegation mechanism
SSM personnel management system
MySQL has no collation factor of order by
Using MATLAB to realize: Jacobi, Gauss Seidel iteration
spark sql任务性能优化(基础)
Oracle segment advisor, how to deal with row link row migration, reduce high water level
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK