Mysql uses regular expressions to filter data _mysql

Source: Internet
Author: User

The difference between regular and like
MySQL's regular expression only makes a subset of the SQL language, and can match the basic characters, strings.
For example: SELECT * from wp_posts where post_name REGEXP ' Hello ' to retrieve all rows containing hello in the column post_name

REGEXP '. og ' . is a special character in the regular expression. It represents matching a character, so dog,hog,mog and so on can match.

Attention:

About the difference between like and regexp: Kind matches the entire column. if the matched text appears only in the column value , like does not find it, the corresponding row is not returned (except, of course, with wildcards). and RegExp matches within the column value, if the matched matching text appears in the column value, RegExp will find it, and the corresponding row is returned, a very important difference (of course, if you adapt to the glyphs ^ and $, you can implement regexp matching the entire column rather than a subset of the columns).

Case-by-case distinction: MySQL regular expression matching (after version 3.23.4) is case-insensitive. If you want to be case-sensitive, you should use the BINARY keyword, such as where Post_name REGEXP BINARY ' hello.000 '

Two, basic character matching
Retrieves the column prod_name all lines that contain text 1000:

Make or Match
equivalent to: or operation "|"

Match one of several characters
just want to match a specific character. You can do this by specifying a set [ of characters to use and ] enclose.

[456]Defines a set of characters that he means to match 4 or 5 or 6. []is another form of an or statement. [456]is [4|5|6] the abbreviation. Match Range
[1-3] a-z is a legitimate area.

Match Special characters

Regular expression languages consist of special characters of special meaning.

.To represent any one character in a regular expression

For example, match the line that contains the string in the Prod_name on :

How does that match,, . [] | , - ?

In order to match Special characters, you must use \\ a leading. For example, to \\. find·

Match character class

Match multiple instances

For example, match the 4-digit number that is linked together :

sticks?: The s latter is optional because it matches 0 or 1 occurrences of any character immediately preceding it.

[:digit:]Matches any number, and thus it is a collection of numbers. {4}exactly requires that the characters in front of it appear 4 times.
So [:digit:]{4} match any 4-digit number that is connected together.

Third, the locator character
All of the examples so far have been to match any text in a string. To match a specific text, you need to use a locator character.

You can also test the correctness of a regular expression without using a database:

SELECT to test the regular expression, the RegExp check always returns 0 or 1,

SELECT ' HELLO ' REGEXP ' 0 '//back to 0

The above is the entire content of this article, I hope to help you learn.

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.