search
HomeDatabaseMysql TutorialMySQL正则表达式使用MySQL系列(4)

MySQL正则表达式使用——MySQL系列(四) 使用REGEXP关键字 1、基本字符匹配 SELECT prod_nameFrom ProductsWhere prod_name REGEXP'.000' LIKE和REGEXP区别 LIKE会匹配这个列,而REGEXP会在列内进行匹配 在MySQL中正则表达式不区分大小写,要区分需使用BINAR

MySQL正则表达式使用——MySQL系列(四)

使用REGEXP关键字

1、基本字符匹配

SELECT prod_name
From Products
Where prod_name REGEXP'.000'

LIKE和REGEXP区别

LIKE会匹配这个列,而REGEXP会在列值内进行匹配

在MySQL中正则表达式不区分大小写,要区分需使用BINARY关键字,如Where prod_name REGEXP BINARY'Jet.000'

2、进行OR匹配

使用“|”

SELECT prod_name
From Products
Where prod_name REGEXP'1000|2000'

3、匹配几个字符之一

指定一组用[和]扩起来的字符

SELECT prod_name
From Products
Where prod_name REGEXP'[123]ton'

输出
1ton
2ton

4、匹配范围

[0-9]数字0到9
[a-z]a到z
[A-Z]A到Z   

[^0-9] ^表示非,即匹配不是0-9           
注意,后面的必须比前面大

SELECT prod_name
From Products
Where prod_name REGEXP'[0-9]ton'

5、匹配特殊字符

在特殊字符前加“\\”进行转义,注意在一般情况下正则表达式的转义加一个“\”就可以了,在MySQL中需要加两个

SELECT prod_name
From Products
Where prod_name REGEXP'\\.000'

输出
1.000ton



6、匹配字符类(Posix字符类)

使用的时候需要外面加一层[],例如[[:digit:]]

说明
[:alnum:] 任意字母和数字(同[a-zA-Z0-9])
[:alpha:] 任意字母(同[a-zA-Z])
[:blank:] 空格和制表(同[\\t])
[:cntrl:] ASCII控制字符(ASCII0到31和127)
[:digit:] 任意数字(同[0-9])
[:graph:] [[:print:]]相同,但不包含空格
[:lower:] 任意小写字母(同[a-z])
[:print:] 任意可打印字符
[:punct:] 即不在[[:alnum:]]又不在[[:cntrl:]]中的字符
[:space:] 包括空格在内的任意空白字符(同[\\f\\n\\r\\t\\v])
[:upper:] 任意大写字母(同[A-Z])
[:xdigit:] 任意16进制数字(同[a-fA-F0-9])
SELECT * FROM `mytable`
Where name REGEXP'name[[:digit:]]';

输出 name1 name6

7、匹配多个实例

元字符 说明
* 0个或多个匹配
+ 1个或多个匹配
0个或1个匹配
{n} 指定数目的匹配
{n,} 不少于指定数目的匹配
{n,m} 匹配数目的范围(m不超过255)

SELECT prod_name
From Products
Where prod_name REGEXP'[0-9]{1,3}'

输出 100 15

8、定位符

元字符 说明
^ 文本的开始
$ 文本的结尾
[[:<:> 词的开始
[[:>:]] 词的结尾

注意,^有两个用法,一个是非,一个是文本的开始,用[]中表示非,否则是文本的开始。




Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment