MySQL学习足迹记录07--数据过滤--用正则表达式进行检索_MySQL
正则表达式
bitsCN.comMySQL学习足迹记录07--数据过滤--用正则表达式进行检索
本文用到的检索数据
mysql> SELECT prod_name FROM products -> ORDER BY prod_name;+----------------+| prod_name |+----------------+| .5 ton anvil || 1 ton anvil || 2 ton anvil || Bird seed || Carrots || Detonator || Fuses || JetPack 1000 || JetPack 2000 || Oil can || Safe || Sling || TNT (1 stick) || TNT (5 sticks) |+----------------+14 rows in set (0.00 sec)
1.基本字符匹配
eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '1000' #匹配"1000" -> ORDER BY prod_name;+--------------+| prod_name |+--------------+| JetPack 1000 |+--------------+1 row in set (0.00 sec) eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '.000' #'.'表示匹配任意一个字符 -> ORDER BY prod_name;+--------------+| prod_name |+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.00 sec)
2.进行OR匹配
为了搜索N个串之一,使用 ‘|’
eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '1000 | 2000' -> ORDER BY prod_name;+--------------+| prod_name |+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.00 sec)
3.匹配几个字符之一
*匹配特定的单字符,可以通过指定一组【】括起来的字符来完成
eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '[123] Ton' -> ORDER BY prod_name;+-------------+| prod_name |+-------------+| 1 ton anvil || 2 ton anvil |+-------------+2 rows in set (0.00 sec) 等效于: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '[1|2|3] Ton' -> ORDER BY prod_name;+-------------+| prod_name |+-------------+| 1 ton anvil || 2 ton anvil |+-------------+2 rows in set (0.00 sec)
4.否定一个字符集‘^'
eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '[^123] Ton' -> ORDER BY prod_name;+--------------+| prod_name |+--------------+| .5 ton anvil |+--------------+1 row in set (0.00 sec)
5.匹配范围【n-m】
eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '[1-5] Ton' -> ORDER BY prod_name;+--------------+| prod_name |+--------------+| .5 ton anvil || 1 ton anvil || 2 ton anvil |+--------------+3 rows in set (0.00 sec)
注:以下操作所用到的表格数据
mysql> SELECT vend_name FROM vendors ORDER BY vend_name;+----------------+| vend_name |+----------------+| ACME || Anvils R Us || Furball Inc. || Jet Set || Jouets Et Ours || LT Supplies |+----------------+6 rows in set (0.00 sec)
6.匹配特殊字符,需用//为前导,即转义字符
*MySQL要求两个反斜杠(MySQL自己解释一个,正则表达式库解释另一个)
匹配'.'
eg: mysql> SELECT vend_name FROM vendors -> WHERE vend_name REGEXP '.' #未用转义字符,所以不是期望的结果 -> ORDER BY vend_name;+----------------+| vend_name |+----------------+| ACME || Anvils R Us || Furball Inc. || Jet Set || Jouets Et Ours || LT Supplies |+----------------+6 rows in set (0.00 sec) 正确的应为: mysql> SELECT vend_name FROM vendors -> WHERE vend_name REGEXP '//.' -> ORDER BY vend_name;+--------------+| vend_name |+--------------+| Furball Inc. |+--------------+1 row in set (0.00 sec)
#以下7,8,9,10列出的仅作参考,无需记忆
7.匹配字符类
[:alnum:] ==> [a-zA-Z0-9]
[:alpha:] ==> [a-zA-Z]
[:blank:] ==>空格和制表符[//t]
[:cntrl:] ==>ASCII控制字符(ASCII0到31和127)
[:digit:] ==>[0-9]
[:graph:] ==>与[:print:]相同,但不包括空格
[:lower:] ==>[a-z]
[:punct:] ==>即不在[:alnum:]又不在[:cntrl:]中的任意字符
[:space:] ==>包括空格在内的任意空白字符[//f//n//r//t//v]
[:upper:] ==>[A-Z]
[:xdigit:]==>任意十六进制数[a-fA-F0-9]
[:print:] ==>任意可打印字符
8.空白元字符
//f ==>换页
//r ==>回车
//v ==>纵向制表
9.匹配多个实例
重复元字符
* ==> 0个或多个匹配
+ ==> 1个或多个匹配(等于{1,})
? ==> 0个或1个匹配(等于{0,1})
{n} ==> 指定数目的匹配
{n,} ==> 不小于指定数目的匹配
{n,m} ==> 匹配数目的范围(m<=255)
eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '//([0-9] sticks?//) ' #'?'匹配它前面的任何字符的0次或1次出现 -> ORDER BY prod_name; +----------------+| prod_name |+----------------+| TNT (1 stick) || TNT (5 sticks) |+----------------+2 rows in set (0.00 sec) mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '[[:digit:]]{4}' #匹配连在一起的任意四位数字 -> ORDER BY prod_name;+--------------+| prod_name |+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.00 sec)
10.定位符
*定位元字符
^ ==> 文本的开始
$ ==> 文本的结尾
[[:<:]] ==> 词的开始
[[:>:]] ==> 词的结尾
eg: mysql> SELECT prod_name FROM products -> WHERE prod_name REGEXP '^[0-9//.]' #'^'定位到串开头,[0-9//.]表示只有在'.'或任一数字为 -> ORDER BY prod_name; #串中的第一个字符,才匹配它+--------------+| prod_name |+--------------+| .5 ton anvil || 1 ton anvil || 2 ton anvil |+--------------+3 rows in set (0.00 sec)
11.'^'的双重用途:在集合'[]'中用来否定集合,否则,用来指串的开始处
bitsCN.com
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
