MySQL正则表达式的描述
正则表达式(regex)是定义复杂查询的一个强有力的工具。 这里是一个简单的资料,它忽略了一些详细的信息。 正则表达式定义了一个字符串的规则。最简单的正则表达式不包含任何保留字。例如,正则表达式hello只和字符串“hello”匹配。 一般的正则表达式使用了
正则表达式(regex)是定义复杂查询的一个强有力的工具。
这里是一个简单的资料,它忽略了一些详细的信息。
正则表达式定义了一个字符串的规则。最简单的正则表达式不包含任何保留字。例如,正则表达式hello只和字符串“hello”匹配。
一般的正则表达式使用了某些特殊的结构,所以它能匹配更多的字符串。例如,正则表达式hello|word既能匹配字符串“hello”也能匹配字符串 “word”。举一个更复杂一点的例子,正则表达式B[an]*s可以匹配字符串“Bananas”、“Baaaaas” 、“Bs”以及其他任何以B开头以s结尾的字符串,中间可以包括任意个a和任意个n的组合。
一个正则表达式中的可以使用以下保留字
^
所匹配的字符串以后面的字符串开头
mysql> select "fonfo" REGEXP "^fo$"; -> 0(表示不匹配)
mysql> select "fofo" REGEXP "^fo"; -> 1(表示匹配)
$
所匹配的字符串以前面的字符串结尾
mysql> select "fono" REGEXP "^fono$"; -> 1(表示匹配)
mysql> select "fono" REGEXP "^fo$"; -> 0(表示不匹配)
.
匹配任何字符(包括新行)
mysql> select "fofo" REGEXP "^f.*"; -> 1(表示匹配)
mysql> select "fonfo" REGEXP "^f.*"; -> 1(表示匹配)
a*
匹配任意多个a(包括空串)
mysql> select "Ban" REGEXP "^Ba*n"; -> 1(表示匹配)
mysql> select "Baaan" REGEXP "^Ba*n"; -> 1(表示匹配)
mysql> select "Bn" REGEXP "^Ba*n"; -> 1(表示匹配)
a+
匹配任意多个a(不包括空串)
mysql> select "Ban" REGEXP "^Ba+n"; -> 1(表示匹配)
mysql> select "Bn" REGEXP "^Ba+n"; -> 0(表示不匹配)
a?
匹配一个或零个a
mysql> select "Bn" REGEXP "^Ba?n"; -> 1(表示匹配)
mysql> select "Ban" REGEXP "^Ba?n"; -> 1(表示匹配)
mysql> select "Baan" REGEXP "^Ba?n"; -> 0(表示不匹配)
de|abc
匹配de或abc
mysql> select "pi" REGEXP "pi|apa"; -> 1(表示匹配)
mysql> select "axe" REGEXP "pi|apa"; -> 0(表示不匹配)
mysql> select "apa" REGEXP "pi|apa"; -> 1(表示匹配)
mysql> select "apa" REGEXP "^(pi|apa)$"; -> 1(表示匹配)
mysql> select "pi" REGEXP "^(pi|apa)$"; -> 1(表示匹配)
mysql> select "pix" REGEXP "^(pi|apa)$"; -> 0(表示不匹配)
(abc)*
匹配任意多个abc(包括空串)
mysql> select "pi" REGEXP "^(pi)*$"; -> 1(表示匹配)
mysql> select "pip" REGEXP "^(pi)*$"; -> 0(表示不匹配)
mysql> select "pipi" REGEXP "^(pi)*$"; -> 1(表示匹配)
{1}
{2,3}
这是一个更全面的方法,它可以实现前面好几种保留字的功能
a*
可以写成a{0,}
a+
可以写成a{1,}
a?
可以写成a{0,1}
在 {}内只有一个整型参数i,表示字符只能出现i次;在{}内有一个整型参数i,后面跟一个“,”,表示字符可以出现i次或i次以上;在{}内只有一个整型参数i,后面跟一个“,”,再跟一个整型参数j,表示字符只能出现i次以上,j次以下(包括i次和j次)。其中的整型参数必须大于等于0,小于等于 RE_DUP_MAX(默认是255)。如果有两个参数,第二个必须大于等于第一个
[a-dX]
匹配“a”、“b”、“c”、“d”或“X”
[^a-dX]
匹配除“a”、“b”、“c”、“d”、“X”以外的任何字符。“[”、“]”必须成对使用
mysql> select "aXbc" REGEXP "[a-dXYZ]"; -> 1(表示匹配)
mysql> select "aXbc" REGEXP "^[a-dXYZ]$"; -> 0(表示不匹配)
mysql> select "aXbc" REGEXP "^[a-dXYZ]+$"; -> 1(表示匹配)
mysql> select "aXbc" REGEXP "^[^a-dXYZ]+$"; -> 0(表示不匹配)
mysql> select "gheis" REGEXP "^[^a-dXYZ]+$"; -> 1(表示匹配)
mysql> select "gheisa" REGEXP "^[^a-dXYZ]+$"; -> 0(表示不匹配)
------------------------------------------------------------
[[.characters.]]
表示比较元素的顺序。在括号内的字符顺序是唯一的。但是括号中可以包含通配符, 所以他能匹配更多的字符。举例来说:正则表达式[[.ch.]]*c匹配chchcc的前五个字符。
[=character_class=]
表示相等的类,可以代替类中其他相等的元素,包括它自己。例如,,如果o和(+)是一个相等的类的成员,那么[[=o=]]、[[=(+)=]]和[o(+)]是完全等价的。
[:character_class:]
在括号里面,在[:和:]中间是字符类的名字,可以代表属于这个类的所有字符。
字符类的名字有: alnum、digit、punct、alpha、graph、space、blank、lower、upper、cntrl、print和xdigit
mysql> select "justalnums" REGEXP "[[:alnum:]]+"; -> 1(表示匹配)
mysql> select "!!" REGEXP "[[:alnum:]]+"; -> 0(表示不匹配)
[[::]]
分别匹配一个单词开头和结尾的空的字符串,这个单词开头和结尾都不是包含在alnum中的字符也不能是下划线。
mysql> select "a word a" REGEXP "[[::]]"; -> 1(表示匹配)
mysql> select "a xword a" REGEXP "[[::]]"; -> 0(表示不匹配)
mysql> select "weeknights" REGEXP "^(wee|week)(knights|nights)$"; -> 1(表示匹配)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
