1. Introduction Regular expression learning website: www.runoob.com/regexp/reg… Regular expression online test: c.runoob.com/front-end/8… It is worth noting that the regular expressions supported by For example, as follows: mysql> select 'I love you China' regexp 'I love you'; +------------------------------+ | 'I love you China' regexp 'I love you' | +------------------------------+ | 1 | +------------------------------+ mysql> select '12306' regexp '[:digit:]'; +----------------------------+ | '12306' regexp '[:digit:]' | +----------------------------+ | 1 | +----------------------------+ 2. Prepare a product table First, prepare a SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for product -- ---------------------------- DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `product_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'Product name', `price` decimal(10, 2) UNSIGNED NOT NULL COMMENT 'Product price', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of product -- ---------------------------- INSERT INTO `product` VALUES (1, 'Apple iPhone 13 (A2634)', 6799.00); INSERT INTO `product` VALUES (2, 'HUAWEI P50 Pro', 6488.00); INSERT INTO `product` VALUES (3, 'MIX4', 4999.00); INSERT INTO `product` VALUES (4, 'OPPO Find X3', 3999.00); INSERT INTO `product` VALUES (5, 'vivo X70 Pro+', 5999.00); SET FOREIGN_KEY_CHECKS = 1; The initial data looks like this: mysql> select * from product; +----+-------------------------+---------+ | id | product_name | price | +----+-------------------------+---------+ | 1 | Apple iPhone 13 (A2634) | 6799.00 | | 2 | HUAWEI P50 Pro | 6488.00 | | 3 | MIX4 | 4999.00 | | 4 | OPPO Find X3 | 3999.00 | | 5 | vivo X70 Pro+ | 5999.00 | +----+-------------------------+---------+ 2.1 Statement Order The function of regular expressions is text matching. By comparing a regular expression with a text content, you can verify whether the text conforms to the rules described by the regular expression. In need: Query the product table for products whose product names contain 3 Statement: mysql> select * from product where product_name regexp '3'; result: +----+-------------------------+---------+ | id | product_name | price | +----+-------------------------+---------+ | 1 | Apple iPhone 13 (A2634) | 6799.00 | | 4 | OPPO Find X3 | 3999.00 | +----+-------------------------+---------+ 2.2 How to distinguish between uppercase and lowercase letters need: Search the product table for products whose product names contain Huawei Statement: mysql> select * from product where product_name regexp 'huawei'; result: +----+----------------+---------+ | id | product_name | price | +----+----------------+---------+ | 2 | HUAWEI P50 Pro | 6488.00 | +----+----------------+---------+ At this time, the query results are case-insensitive by default, so they can be queried directly. If we want the query to be case-sensitive, we only need to add the Statement: mysql> select * from product where product_name regexp binary 'huawei'; result: Empty set (0.00 sec) Since 2.3 The difference between regular expressions and likeI believe some of you have discovered that the functions implemented above can actually be achieved using like. In many scenarios, we use like to match strings, but these scenarios are often very simple. Regular expressions are a very powerful text retrieval and filtering tool, and the functions they can achieve are much more powerful than the like operator. In short, anything that LIKE can do with regular expressions, basically nothing that regular expressions can do with LIKE (or it's very tricky). For example, the following requirement can be easily implemented using regular expressions, but I don’t know how to implement the like operator. need: Query the product table, and find the product information where v appears at least once in the product name Statement: mysql> select * from product where product_name regexp 'v+'; result: +----+---------------+---------+ | id | product_name | price | +----+---------------+---------+ | 5 | vivo X70 Pro+ | 5999.00 | +----+---------------+---------+
Repeating Metacharacters
You may also be interested in:
|
<<: Flame animation implemented with CSS3
>>: A solution to a bug in IE6 with jquery-multiselect
1. In the previous chapter, we learned that we ca...
1. Download the tomcat image docker pull tomcat:8...
Today, after the game was restarted, I found that...
1. Introduction Why do we need indexes? In genera...
html, address,blockquote,body, dd, div,dl, dt, fie...
The meaning of key_len In MySQL, you can use expl...
Mobile Mobile pages only need to be compatible wi...
Table of contents 1. Definition of stack 2. JS st...
Preface Sometimes when we view database data, we ...
Let's take a look at the detailed method of b...
usemap is an attribute of the <img> tag, use...
1. Background Generally, in a data warehouse envi...
In the XHTML language, we all know that the ul ta...
Cross-domain solutions jsonp (simulate get) CORS ...
Table of contents Preface Install vue-i18n Config...