The use method of the regular expression of MySQL description _php skill

Source: Internet
Author: User
Tags character classes reserved

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.