How to use regular expression query in MySql

How to use regular expression query in MySql

Regular expressions are often used to search and replace text that matches a certain pattern. For example, extracting phone numbers from a text file, finding repeated words in an article, or replacing certain sensitive words entered by the user. MySQL uses the REGEXP keyword to specify a regular expression character matching pattern.

目錄

1. The character '^' searches for records that begin with a specific character or string

SELECT * FROM user WHERE email REGEXP '^a'

The character '^' matches records that begin with a specific character or string. The above statement queries records whose mailboxes begin with a

2. Characters' Query records ending with a specific character or string

SELECT * FROM user WHERE phone REGEXP '0$'

The character '$' matches records ending with a specific character or string. The above statement queries records ending with 0 in the mailbox.

3. Use the symbol "." to replace any character in the string

SELECT * FROM user WHERE email REGEXP 'a.c'

Query the records with a character between mailboxes a and c. '.' is equivalent to a placeholder. If it is written as REGEXP 'a..c', that is, there are two dots between a and c, it means that there must be two characters between a and c in the mailbox.

4. Use "*" to match multiple characters

SELECT * FROM user WHERE email REGEXP 'm*'

Query all mailboxes containing m.

SELECT * FROM user WHERE email REGEXP '^am*'

Query the mailboxes that start with letter a and are followed by letter m. Where '*' means 0 or more times.

5. Use the character "+" to represent the following character

SELECT * FROM user WHERE email REGEXP 'm+'

Query all mailboxes containing m.

SELECT * FROM user WHERE email REGEXP '^am+'

Query the mailboxes that start with the letter a, followed by the letter m. Where '+' means the character that follows.

6. “|” separates the conditions to match the specified string

SELECT * FROM user WHERE email REGEXP 'qq.com|163.com'

Regular expressions can match specified strings, and strings are separated by "|".

7. “[]” means the set matches any one of the specified strings

SELECT * FROM user WHERE email REGEXP '[az]'

"[]" specifies a set. The above means querying mailboxes with a or z or both. It can also be used to match a set of numbers, for example, [0-9] means all numbers in the set, and [az] means all letters in the set.

8. “[^]” matches any character other than the specified character

SELECT * FROM user WHERE email REGEXP '[^a-d1-3]'

The above matches the records that do not contain a, b, c, d and do not contain 1, 2, 3 in the mailbox.

9. Use {n,} or {n,m} to specify the number of times the string concatenation occurs

SELECT * FROM user WHERE email REGEXP 'b{2}'

Indicates that the letter b appears at least 2 times.

SELECT * FROM user WHERE email REGEXP 'yu{1,3}'

It means that the string ba appears at least once and at most three times.

Summarize

The above is the method of using regular expression query in MySql introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to use regular expression query in mysql
  • mysql regular expression query contains non-digits and characters records
  • Detailed explanation of MySql basic query, join query, subquery, regular expression query
  • Summary of classic examples of MySQL query operations using regular expressions
  • MySql official manual study notes 2 MySql fuzzy query and regular expression
  • MySQL complete collapse query regular matching detailed explanation

<<:  Learn the key knowledge that must be mastered in the Vue framework

>>:  Summary of shell's method for determining whether a variable is empty

Recommend

A brief analysis of crontab task scheduling in Linux

1. Create a scheduling task instruction crontab -...

Example of using supervisor to manage nginx+tomcat containers

need: Use docker to start nginx + tomcat dual pro...

The pitfalls encountered when learning Vue.js

Table of contents Class void pointing ES6 Arrow F...

Pygame code to make a snake game

Table of contents Pygame functions used Creating ...

Beginners learn some HTML tags (3)

Beginners who are exposed to HTML learn some HTML...

Three ways to forward linux ssh port

ssh is one of the two command line tools I use mo...

How to Change Colors and Themes in Vim on Linux

Vim is a text editor that we use very often in Li...

Use of Linux ipcs command

1. Command Introduction The ipcs command is used ...

How Web Designers Create Images for Retina Display Devices

Special statement: This article is translated bas...

Mysql multi-condition query statement with And keyword

MySQL multi-condition query with AND keyword. In ...

How to configure VMware virtual machine NAT mode

This article describes the VMware virtual machine...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

A brief discussion on using Vue to complete the mobile apk project

Table of contents Basic Configuration Entry file ...