Detailed explanation of the use of MySQL comparison operator regular expression matching REGEXP

Detailed explanation of the use of MySQL comparison operator regular expression matching REGEXP

1. Initialize data

DROP TABLE IF EXISTS `test_01`;
CREATE TABLE `test_01` (
 `id` int(0) NOT NULL,
 `stu` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Student number',
 `user` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'User',
 `km` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Subject',
 `fs` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Score',
 `time` datetime(0) NULL DEFAULT NULL COMMENT 'time',
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

INSERT INTO `test_01` VALUES (1, 'X0219001', '小三', '语文', '98', '2020-08-06 15:51:21');
INSERT INTO `test_01` VALUES (2, 'X0219001', '小三', '数学', '90', '2020-07-01 15:51:25');
INSERT INTO `test_01` VALUES (3, 'x0219001', '小三', '英语', '77', '2020-06-01 15:51:28');
INSERT INTO `test_01` VALUES (4, 'X0219002', '小二', '语文', '98', '2020-08-06 15:51:21');

1. Basic character matching

Matches student numbers that contain 'X' in the field. Case insensitive

SELECT * FROM test_01 WHERE stu REGEXP 'X';

insert image description here

2. '.' means matching any character

If you need to match multiple characters, add more dots.

SELECT * FROM test_01 WHERE stu REGEXP '.9001';
SELECT * FROM test_01 WHERE stu REGEXP '.02..0';

insert image description here

3. '|' means to search for one of the two strings

SELECT * FROM test_01 WHERE user REGEXP '二|四';

insert image description here

4. '[ ]' matches any single character

SELECT * FROM test_01 WHERE stu REGEXP '0[23]';

Here [23] is equivalent to [2|3], and one [] matches one character.

insert image description here

Matching range
[0123456789] or [0-9] will match the digits 0 to 9
[az] matches any alphabetic character

5. Matching special characters

1.\ escape character

That is, escaping: all characters that have special meaning within a regular expression must be escaped in this way.

Metacharacters illustrate
\\- Indicates search-
\\. Indicates search.

2.\ is also used to quote metacharacters

Metacharacters illustrate
\f Page Break
\n Line Break
\r Enter
\t tabulation
\v Vertical tabulation

3. Match multiple instances

Metacharacters illustrate
* 0 or more matches
+ 1 or more matches (equal to {1, })
? 0 or 1 matches (equal to {0, 1})
{n} Specified number of matches
{n, } At least the specified number of matches
{n,m} The range of matching numbers (m does not exceed 255)

4. Matching Character Classes

Code explain
[:a;num:] Any letters and numbers (same as [a-zA-Z0-9])
[:alpha:] Any character (same as [a-zA-Z])
[:blank:] Spaces and tabs (same as [\t])
[:cntrl:] ASCII control characters (ASCII 0 to 31 and 127)
[:digit:] Any number (same as [0-9])
[:graph:] Same as ["print:], but without spaces
[:lower:] Any lowercase line (same as [az])
[:print:] Any printable character
[:punct:] Any character that is not in either [:alnum:] or [:cntrl:]
[space:] Any whitespace character including spaces (same as [\f\n\t\r\v])
[:upper:] Any size letter (same as [AZ])
[:xdigit:] Any hexadecimal number (same as [a-fA-F0-9])

This is the end of this article about the detailed usage of the MySQL comparison operator regular expression matching REGEXP. For more relevant MySQL regular expression matching REGEXP content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MYSQL Operator Summary
  • Introduction to MySQL <> and <=> operators
  • Summary of the use of special operators in MySql
  • Summary of commonly used operators and functions in MySQL
  • MySQL Notes — SQL Operators

<<:  Detailed explanation of how Nginx works

>>:  A brief discussion on the implementation principle of Webpack4 plugins

Recommend

Detailed explanation of the correct way to open em in CSS

Why do we say “usually 1em=16px”? The default tex...

Detailed explanation of InnoDB storage files in MySQL

Physically speaking, an InnoDB table consists of ...

JavaScript timer to achieve limited time flash sale function

This article shares the specific code of JavaScri...

Docker Nginx container production and deployment implementation method

Quick Start 1. Find the nginx image on Docker Hub...

React uses emotion to write CSS code

Table of contents Introduction: Installation of e...

A simple tutorial on how to use the mysql log system

Table of contents Preface 1. Error log 2. Binary ...

Solve the problem of combining AND and OR in MySQL

As shown below: SELECT prod_name,prod_price FROM ...

How to install MySQL and Redis in Docker

This article is based on the CentOS 7.3 system en...

A general method for implementing infinite text carousel with native CSS

Text carousels are very common in our daily life....

HTML Grammar Encyclopedia_HTML Language Grammar Encyclopedia (Must Read)

Volume Label, Property Name, Description 002 <...

Use h1, h2, and h3 tags appropriately

In the process of making web pages, it is inevita...

Some references about colors in HTML

In HTML, colors are represented in two ways. One i...

MySQL uses UNIQUE to implement non-duplicate data insertion

SQL UNIQUE constraint The UNIQUE constraint uniqu...