The preliminary study of MySQL regular expression

Source: Internet
Author: User
Tags regular expression

We know that in the SQL, you can use like this predicate (expression) for fuzzy retrieval, and support for the%,?, _ and other placeholders.

However, the function of this fuzzy search has many limitations, simply too vague.

The REGEXP keyword is provided in MySQL to support regular expressions, of course, just some simple regular.

First, we construct some test data.

?

1 2 3 4 5 6 7 8 9 --Create a table use test; DROP TABLE IF EXISTS T_regcustomer; CREATE TABLE t_regcustomer (id int (a) auto_increment, name VARCHAR (256), Age INT (), PRIMARY KEY (ID)) Collate= ' Utf8_general_ci ' engine=innodb;

Add some test data:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

1. The simplest query:

?

1 2 SELECT * from T_regcustomer;

2. Specify Column name query

?

1 2 3 SELECT c.id, C.name, c.age from T_regcustomer C;

3. Sort the query results

?

1 2 3 4 SELECT c.id, C.name, c.age from T_regcustomer C-order by C.age ASC;

4. Like fuzzy search

% match any number of characters (0~n)

?

1 2 3 4 5 SELECT c.id, C.name, c.age from T_regcustomer c WHERE c.name like '% ROC% ' ORDER by C.age ASC;

5. regexp keyword

. Match any one character

Note that there is no starting (^) and ending ($) qualifier, so as long as the rows that appear in the column are retrieved.

?

1 2 3 4 5 SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP '. Peng. ' ORDER by C.age ASC;

6. Regular Start qualifier

?

1 2 3 4 5 SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP ' ^ King ' ORDER by C.age ASC;

7. Case sensitive

?

1 2 3 4 5 SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP the ' BINARY ' order by ^s ASC;

8. Regular or operational

?

1 2 3 4 5 SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP the ' BINARY ' order by A|s ASC;

9. The group operation is regular

[123] indicates that 1, 2, 3 of these 3 digits appear

?

1 2 3 4 5 SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP ' peng [BINARY] ' ORDER by 123 ASC;

[1-9] matches 1, 2, 3 、.... 8, 9

?

1 2 3 4 5 SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP ' peng [BINARY] ' ORDER by 1-9 ASC;

10. Escape

Use

can be escaped. []()?-| As well as pagination, line-changing symbols, etc.

11. More Content

Please refer to the MySQL must-know-will 68-page regular 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.