当前位置:网站首页>Detailed explanation of regular expression (regexp) in MySQL
Detailed explanation of regular expression (regexp) in MySQL
2022-07-06 23:19:00 【1024 questions】
Preface
LIKE and REGEXP The difference between
OR Match
Match range
Match special character
Match character class
Match multiple instances
Locator
^ There are two uses
attach :Mysql String interception and Intercept characters for query
summary
PrefaceSometimes 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 betweenLIKE 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 Matchselect 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 characterThen 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 classSometimes 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 instancesSometimes 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 }).
LocatorSometimes 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[,|;]?';
attach :Mysql String interception and Intercept characters for query One 、MySQL Interception of strings in
MySQL There is a special string interception function in : There are two commonly used :substring_index(str,delim,count) and concat
1.substring_index(str,delim,count) Functions are commonly used .
The functions in parentheses are : To separate the intercepted string ( Such as :”aaa_bbb_ccc”)、 Separator ( Such as :“_”)、 Location ( Indicates the number of separators , Such as :“1”).
count Is a positive number , Then count from the left , Function returns the second count String to the left of separator ;
count It's a negative number , So it's counting from the right , Function returns the second count Everything to the right of the separator ;
count It can be for 0, Return to empty .
Example :substring_index("aaa_bbb_ccc","_",1) , Return to aaa;
substring_index("aaa_bbb_ccc","_",2) , Return to aaa_bbb;
substring_index(substring_index("aaa_bbb_ccc","_",-2),"_",1) , Return to bbb;
2.concat Is to connect several strings
Example :concat(‘m’,’y’,’s’,’q’,’l’);
return :mysql
Two 、 Query all data containing this character according to a field in the table
1.find_in_set:SELECT * FROM user WHERE find_in_set(‘ Wu ’,name);
Inquire about user All in the table name contain “ Wu ” The data of
2.REGEXP:SELECT * FROM user WHERE name REGEXP ‘(‘ Wu ’|‘ Liu ’)’;
Use regular , Query data containing Liu or Wu
summaryThis is about MySQL Regular expressions in (REGEXP) This is the end of the article , More about MySQL For regular expression content, please search the previous articles on SDN or continue to browse the following related articles. I hope you can support SDN in the future !
边栏推荐
- How to achieve text animation effect
- 儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
- C three ways to realize socket data reception
- Redis 持久化机制
- 借助这个宝藏神器,我成为全栈了
- [launched in the whole network] redis series 3: high availability of master-slave architecture
- UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
- Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
- 问下各位,有没有flink sql生成作业的文档啊或是案列啊知道flink cli可以建表和指定目
- Mysql 身份认证绕过漏洞(CVE-2012-2122)
猜你喜欢
Docker starts MySQL and -emysql_ ROOT_ Password = my secret PW problem solving
js对JSON数组的增删改查
With the help of this treasure artifact, I became the whole stack
儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
Custom swap function
为了交通安全,可以做些什么?
Let's see through the network i/o model from beginning to end
【全网首发】Redis系列3:高可用之主从架构的
(flutter2) as import old project error: inheritfromwidgetofexacttype
Modules that can be used by both the electron main process and the rendering process
随机推荐
华为云GaussDB(for Redis)揭秘第21期:使用高斯Redis实现二级索引
How to choose the server system
动作捕捉用于蛇运动分析及蛇形机器人开发
How to choose indoor LED display? These five considerations must be taken into account
Pdf batch splitting, merging, bookmark extraction, bookmark writing gadget
机器人材料整理中的套-假-大-空话
Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
B站大佬用我的世界搞出卷積神經網絡,LeCun轉發!爆肝6個月,播放破百萬
spark调优(二):UDF减少JOIN和判断
案例推荐丨安擎携手伙伴,保障“智慧法院”更加高效
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
Interview question: AOF rewriting mechanism, redis interview must ask!!!
Nftscan Developer Platform launches Pro API commercial services
室内LED显示屏应该怎么选择?这5点注意事项必须考虑在内
docker中mysql开启日志的实现步骤
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务...
How to achieve text animation effect
Children's pajamas (Australia) as/nzs 1249:2014 handling process