Home > Database > Mysql Tutorial > Detailed explanation of query examples using regular expressions in MySql

Detailed explanation of query examples using regular expressions in MySql

小云云
Release: 2018-01-06 14:33:00
Original
1914 people have browsed it

Regular expressions are commonly used to retrieve and replace text that matches the magic pattern. For example, extract a phone number from a text file, find repeated words in an article, or replace certain sensitive words entered by the user. Mysql uses the REGEXP keyword to specify the character matching pattern of a regular expression. Mysql uses the REGEXP keyword to specify the character matching pattern of a regular expression. Next, I will share with you the method of using regular expression query in MySql through this article. Friends who are interested should take a look at it. I hope it can help you.

Detailed explanation of query examples using regular expressions in MySql

1. The character '^' queries for records starting with a specific character or string

SELECT * FROM user WHERE email REGEXP '^a'
Copy after login

The character '^' matches records starting with a specific character or string The above statement queries the records whose mailbox starts with a

2. The character ' queries the records ending with a specific character or string

SELECT * FROM user WHERE phone REGEXP '0$'
Copy after login

The character '$' matches a specific character or string The above statement queries the records whose mailbox ends with 0

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

SELECT * FROM user WHERE email REGEXP 'a.c'
Copy after login

Query between mailbox a and c There is a record of one character, '.' 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*'
Copy after login

Query the records with m in all mailboxes.

SELECT * FROM user WHERE email REGEXP '^am*'
Copy after login

Query the records whose email address starts with the letter a and is followed by the letter m. Where '*' means 0 times or more.

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

SELECT * FROM user WHERE email REGEXP 'm+'
Copy after login

Query the records with m in all mailboxes.

SELECT * FROM user WHERE email REGEXP '^am+'
Copy after login

Query the records whose email address starts with the letter a, followed by the letter m. Where '+' means the following character.

6. "|" delimited condition matches the specified string

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

Regular expression can match the specified string, and the strings are separated by "|".

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

SELECT * FROM user WHERE email REGEXP '[az]'
Copy after login

”[]” specifies a set. The above indicates that the query mailbox contains a or z or both. All have mailboxes. It can also be used to match a set of numbers. For example, [0-9] represents all numbers in the set interval, and [a-z] represents all letters in the set interval.

8. "[^]" matches characters other than the specified characters

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

As shown above, the matching mailbox does not contain a, b, c, d and does not contain 1, 2, 3 records.

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

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

means that the letter b appears at least 2 times.

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

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

Related recommendations:

WeChat applet implements simple input regular expression verification function sharing

How to use regular expressions in Javascript

Regular Expression_Regular Expression in Replace

The above is the detailed content of Detailed explanation of query examples using regular expressions in MySql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template