search
HomeDatabaseMysql Tutorialmysql模式匹配和正则表达式_MySQL

正则表达式

SQL模式匹配

_  下划线匹配任何单个字符

%  匹配任意数码字符

 

正则表达式的匹配

使用REGEXP和NOT REGEXP操作符

 

‘.’匹配任何单个的字符。

字符类“[...]”匹配在方括号内的任何字符。例如,“[abc]”匹配“a”、“b”或“c”。为了命名字符的范围,使用一个“-”。“[a-z]”匹配任何字母,而“[0-9]”匹配任何数字。

“ * ”匹配零个或多个在它前面的字符。例如,“x*”匹配任何数量的“x”字符,“[0-9]*”匹配任何数量的数字,而“.*”匹配任何数量的任何字符。

为了找出以“b”开头的名字,使用“^”匹配名字的开始:

mysql> SELECT * FROM pet WHERE name REGEXP '^b';

SELECT * FROM `student` WHERE `username` REGEXP '1$'

 

为了找出包含一个“w”的名字,使用以下查询:

mysql> SELECT * FROM pet WHERE name REGEXP 'w';

 

你也可以使用“{n}”“重复n次”操作符重写前面的查询:

mysql> SELECT * FROM pet WHERE name REGEXP '^.{5}$';






对于REGEXP操作符,正则表达式可以使用任何下述特殊字符和结构:

·         ^

匹配字符串的开始部分。

mysql> <strong>SELECT 'fo/nfo' REGEXP '^fo$';</strong>                   -> 0
mysql> <strong>SELECT 'fofo' REGEXP '^fo';</strong>                      -> 1

·         $

匹配字符串的结束部分。

mysql> <strong>SELECT 'fo/no' REGEXP '^fo/no$';</strong>                 -> 1
mysql> <strong>SELECT 'fo/no' REGEXP '^fo$';</strong>                    -> 0

·         .

匹配任何字符(包括回车和新行)。

mysql> <strong>SELECT 'fofo' REGEXP '^f.*$';</strong>                    -> 1
mysql> <strong>SELECT 'fo/r/nfo' REGEXP '^f.*$';</strong>                -> 1

·         a*

匹配0或多个a字符的任何序列。

mysql> <strong>SELECT 'Ban' REGEXP '^Ba*n';</strong>                     -> 1
mysql> <strong>SELECT 'Baaan' REGEXP '^Ba*n';</strong>                   -> 1
mysql> <strong>SELECT 'Bn' REGEXP '^Ba*n';</strong>                      -> 1

·         a+

匹配1个或多个a字符的任何序列。

mysql> <strong>SELECT 'Ban' REGEXP '^Ba+n';</strong>                     -> 1
mysql> <strong>SELECT 'Bn' REGEXP '^Ba+n';</strong>                      -> 0

·         a?

匹配0个或1个a字符。

mysql> <strong>SELECT 'Bn' REGEXP '^Ba?n';</strong>                      -> 1
mysql> <strong>SELECT 'Ban' REGEXP '^Ba?n';</strong>                     -> 1
mysql> <strong>SELECT 'Baan' REGEXP '^Ba?n';</strong>                    -> 0

·         de|abc

匹配序列de或abc。

mysql> <strong>SELECT 'pi' REGEXP 'pi|apa';</strong>                     -> 1
mysql> <strong>SELECT 'axe' REGEXP 'pi|apa';</strong>                    -> 0
mysql> <strong>SELECT 'apa' REGEXP 'pi|apa';</strong>                    -> 1
mysql> <strong>SELECT 'apa' REGEXP '^(pi|apa)$';</strong>                -> 1
mysql> <strong>SELECT 'pi' REGEXP '^(pi|apa)$';</strong>                 -> 1
mysql> <strong>SELECT 'pix' REGEXP '^(pi|apa)$';</strong>                -> 0

·         (abc)*

匹配序列abc的0个或多个实例。

mysql> <strong>SELECT 'pi' REGEXP '^(pi)*$';</strong>                    -> 1
mysql> <strong>SELECT 'pip' REGEXP '^(pi)*$';</strong>                   -> 0
mysql> <strong>SELECT 'pipi' REGEXP '^(pi)*$';</strong>                  -> 1

·         {1}, {2,3}

{n}或{m,n}符号提供了编写正则表达式的更通用方式,能够匹配模式的很多前述原子(或“部分”)。m和n均为整数。

o        a*

可被写入为a{0,}。

o        a+

可被写入为a{1,}。

o        a?

可被写入为a{0,1}。

更准确地讲,a{n}与a的n个实例准确匹配。a{n,}匹配a的n个或更多实例。a{m,n}匹配a的m~n个实例,包含m和n。

m和n必须位于0~RE_DUP_MAX(默认为255)的范围内,包含0和RE_DUP_MAX。如果同时给定了m和n,m必须小于或等于n。

mysql> <strong>SELECT 'abcde' REGEXP 'a[bcd]{2}e';</strong>              -> 0
mysql> <strong>SELECT 'abcde' REGEXP 'a[bcd]{3}e';</strong>              -> 1
mysql> <strong>SELECT 'abcde' REGEXP 'a[bcd]{1,10}e';</strong>           -> 1

·         [a-dX], [^a-dX]

匹配任何是(或不是,如果使用^的话)a、b、c、d或X的字符。两个其他字符之间的“-”字符构成一个范围,与从第1个字符开始到第2个字符之间的所有字符匹配。例如,[0-9]匹配任何十进制数字 。要想包含文字字符“]”,它必须紧跟在开括号“[”之后。要想包含文字字符“-”,它必须首先或最后写入。对于[]对内未定义任何特殊含义的任何字符,仅与其本身匹配。

mysql> <strong>SELECT 'aXbc' REGEXP '[a-dXYZ]';</strong>                 -> 1
mysql> <strong>SELECT 'aXbc' REGEXP '^[a-dXYZ]$';</strong>               -> 0
mysql> <strong>SELECT 'aXbc' REGEXP '^[a-dXYZ]+$';</strong>              -> 1
mysql> <strong>SELECT 'aXbc' REGEXP '^[^a-dXYZ]+$';</strong>             -> 0
mysql> <strong>SELECT 'gheis' REGEXP '^[^a-dXYZ]+$';</strong>            -> 1
mysql> <strong>SELECT 'gheisa' REGEXP '^[^a-dXYZ]+$';</strong>           -> 0

·         [.characters.]

在括号表达式中(使用[和]),匹配用于校对元素的字符序列。字符为单个字符或诸如新行等字符名。在文件regexp/cname.h中,可找到字符名称的完整列表。

mysql> <strong>SELECT '~' REGEXP '[[.~.]]';</strong>                     -> 1
mysql> <strong>SELECT '~' REGEXP '[[.tilde.]]';</strong>                 -> 1

·         [=character_class=]

在括号表达式中(使用[和]),[=character_class=]表示等同类。它与具有相同校对值的所有字符匹配,包括它本身,例如,如果o和(+)均是等同类的成员,那么[[=o=]]、[[=(+)=]]和[o(+)]是同义词。等同类不得用作范围的端点。

·         [:character_class:]

在括号表达式中(使用[和]),[:character_class:]表示与术语类的所有字符匹配的字符类。标准的类名称是:

alnum

文字数字字符

alpha

文字字符

blank

空白字符

cntrl

控制字符

digit

数字字符

graph

图形字符

lower

小写文字字符

print

图形或空格字符

punct

标点字符

space

空格、制表符、新行、和回车

upper

大写文字字符

xdigit

十六进制数字字符

它们代表在ctype(3)手册页面中定义的字符类。特定地区可能会提供其他类名。字符类不得用作范围的端点。

mysql> <strong>SELECT 'justalnums' REGEXP '[[:alnum:]]+';</strong>       -> 1







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
Reduce the use of MySQL memory in DockerReduce the use of MySQL memory in DockerMar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared libraryHow to solve the problem of mysql cannot open shared libraryMar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin)Run MySQl in Linux (with/without podman container with phpmyadmin)Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overviewWhat is SQLite? Comprehensive overviewMar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guideRunning multiple MySQL versions on MacOS: A step-by-step guideMar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)