Description of the MySQL regular expression

Source: Internet
Author: User
Tags character classes empty expression integer mysql regular expression reserved
The mysql| Regular expression (regex) is a powerful tool for defining complex queries.
Here is a simple information that ignores some of the detailed information.
A regular expression defines a rule for a string. The simplest regular expression does not contain any reserved words. For example, the regular expression Hello is only matched to the string "Hello".
A regular expression uses some special structure, so it can match more strings. For example, regular expression Hello|word can match both the string "Hello" and the string "word". To give a more complicated example, the regular expression b[an]*s can match the string "Bananas", "Baaaaas", "Bs", and any other string that begins with B with the end of S, and the middle can include any combination of a and any n.

The following reserved words can be used in a regular expression
^
The matched string starts with the following string
mysql> Select "Fonfo" REGEXP "^fo$"; -> 0 (indicates no match)
mysql> Select "Fofo" REGEXP "^FO"; -> 1 (matching)
$
The matched string ends with the preceding string
Mysql> Select "Fono" REGEXP "^fono$"; -> 1 (matching)
Mysql> Select "Fono" REGEXP "^fo$"; -> 0 (indicates no match)
.
Match any character (including new rows)
mysql> Select "Fofo" REGEXP "^f.*"; -> 1 (matching)
mysql> Select "Fonfo" REGEXP "^f.*"; -> 1 (matching)
A *
Match any number of a (including empty strings)
mysql> Select "Ban" REGEXP "^ba*n"; -> 1 (matching)
mysql> Select "Baaan" REGEXP "^ba*n"; -> 1 (matching)
mysql> Select "Bn" REGEXP "^ba*n"; -> 1 (matching)
A +
Match any number of a (excluding empty strings)
mysql> Select "Ban" REGEXP "^ba+n"; -> 1 (matching)
mysql> Select "Bn" REGEXP "^ba+n"; -> 0 (indicates no match)
A?
Match one or 0 a
mysql> Select "Bn" REGEXP "^ba?n"; -> 1 (matching)
mysql> Select "Ban" REGEXP "^ba?n"; -> 1 (matching)
mysql> Select "Baan" REGEXP "^ba?n"; -> 0 (indicates no match)
De|abc
Match de or ABC
mysql> Select "PI" REGEXP "Pi|apa"; -> 1 (matching)
Mysql> select "Axe" REGEXP "Pi|apa"; -> 0 (indicates no match)
Mysql> Select "APA" REGEXP "Pi|apa"; -> 1 (matching)
Mysql> Select "APA" REGEXP "^ (Pi|apa) $"; -> 1 (matching)
mysql> Select "PI" REGEXP "^ (Pi|apa) $"; -> 1 (matching)
mysql> select "Pix" REGEXP "^ (Pi|apa) $"; -> 0 (indicates no match)
(ABC) *
Match any number of ABC (including Empty strings)
mysql> Select "PI" REGEXP "^ (pi) *$"; -> 1 (matching)
mysql> Select "Pip" REGEXP "^ (pi) *$"; -> 0 (indicates no match)
mysql> Select "Pipi" REGEXP "^ (pi) *$"; -> 1 (matching)
{1}
{2,3}
This is a more comprehensive approach that enables you to implement several of the previous reserved words
A *
Can be written as a{0,}
A +
Can be written as A{1,}
A?
Can be written as a{0,1}
There is only one integer parameter I in {}, which indicates that the character can only appear I; there is an integer parameter I in {}. followed by a ",", indicating that the character can appear I or I more than, in {} Only an integer parameter I, followed by a ",", and then an integer parameter J, indicating that the character can only appear more than I, J Times below (including I and J times). The integer argument must be greater than or equal to 0, and less than or equal to Re_dup_max (default is 255). If there are two parameters, the second must be greater than or equal to the first
[A-DX]
Match "A", "B", "C", "D" or "X"
[^A-DX]
Matches any character other than "A", "B", "C", "D", "X". "[", "]" must be used in pairs
mysql> Select "AxBC" REGEXP "[a-dxyz]"; -> 1 (matching)
mysql> Select "AxBC" REGEXP "^[a-dxyz]$"; -> 0 (indicates no match)
mysql> Select "AxBC" REGEXP "^[a-dxyz]+$"; -> 1 (matching)
mysql> Select "AxBC" REGEXP "^[^a-dxyz]+$"; -> 0 (indicates no match)
mysql> Select "Gheis" REGEXP "^[^a-dxyz]+$"; -> 1 (matching)
mysql> Select "Gheisa" REGEXP "^[^a-dxyz]+$"; -> 0 (indicates no match)
------------------------------------------------------------
[[. characters.]]
Represents the order in which elements are compared. The order of characters within parentheses is unique. But the parentheses can contain wildcards, so he can match more characters. For example: Regular expressions [[. Ch.]] *c matches the first five characters of the CHCHCC.
[=character_class=]
A class that represents equality and can replace other equal elements in a class, including itself. For example, if O and (+) are members of an equal class, then [[=o=]], [[= (+] =]] and [O (+)] are completely equivalent.
[: Character_class:]
Inside the brackets, in [: And:] The middle is the name of the character class, which can represent all characters belonging to this class.
The names of the character classes are: alnum, digit, punct, alpha, graph, space, blank, lower, upper, cntrl, print, and Xdigit
mysql> Select "Justalnums" REGEXP "[: alnum:]]+"; -> 1 (matching)
mysql> Select "!!" REGEXP "[[: alnum:]]+"; -> 0 (indicates no match)
[[::]]
An empty string that matches the beginning and end of a word, and neither the beginning nor the end of the word is a character that is contained in alnum and cannot be underlined.
Mysql> Select "A word a" REGEXP "[[::]]"; -> 1 (matching)
Mysql> Select "A Xword a" REGEXP "[[::]]"; -> 0 (indicates no match)
Mysql> Select "Weeknights" REGEXP "^ (Wee|week) (knights|nights) $"; -> 1 (matching)

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.