The use of the MySQL Express expression

Source: Internet
Author: User

The performance of the normal expression in MySQL is higher than like, so here is a summary of the use of regular expressions.

Patterns of regular expressions and their meanings:

The following examples illustrate their usage:

Build Table Student:

CREATE TABLE student (ID int (6) Auto_increment,name Carchar (6), age int (3), primary key (ID)); Insert data: INSERT into student (name , age) VALUES (' Xzb ', (), (' Spal '), (' WGC ', 32);

1.^

Select name from student where name REGEXP ' ^x '; --querying data that begins with X

2.$

SELECT * FROM student where name REGEXP ' C $ '; --Query for data ending in C

3. ".": This character is the English point, it matches any one character, including carriage return, line wrapping and so on.

SELECT * FROM student where name REGEXP ' X. '; --Match the first letter X, the rest of the letter arbitrary data

4.*: An asterisk matches 0 or more characters and must have content before it.

SELECT * FROM student where name REGEXP ' x* '; --match any of the characters

5. "+": the plus sign matches 1 or more characters, and must have content before it.

SELECT * FROM student where name REGEXP ' x* ';--matches any character greater than 1

6. "?": the question mark matches 0 or 1 times.

SELECT * FROM student where name REGEXP ' x? '; --Match 0 or 1 characters

7.xzb|spal|

SELECT * FROM student where name REGEXP ' Xzb|spal ';--match xzb or Spal

8.[x-z]*

SELECT * FROM student where name REGEXP ' ^[x-z] ';--matches data that begins with a character in X, Y, Z

SELECT * FROM student where name REGEXP ' [a-d]$ ';--match data ending with characters in a-d

9.[^x-z]*

SELECT * FROM student where name REGEXP ' ^[^x-z] ';--matches data that does not begin with a character that is not in the X-z

SELECT * FROM student where name REGEXP ' [^h-j]$ ';--matches data that is not at the beginning of a character in X-z

10.{n}

SELECT * FROM student where name REGEXP ' ^s{2} ';--match all data that starts with S and repeats at least 2 times

11.{N,M}

SELECT * FROM student where name REGEXP ' ^s{2,5} ';--match all data that starts with S and repeats 2 to 5 times

The use of the MySQL Express expression

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.