Description of mysql regular expressions

xiaoxiao2021-03-06  50

Regular expression (regex) is a powerful tool that defines complex queries.

Here is a simple information that ignores some detailed information.

Regular expressions define a string rule. The simplest regular expression does not contain any reserved words. E.g,

Regular expressions Hello only matches strings "Hello".

The general regular expression uses some special structures, so it matches more strings. For example, regular

Expression Hello | WORD can match the string "Hello" and match the string "word".

For a more complicated example, regular expression B [AN] * S can match the string "bananas", "BAAAAA

S "

, "BS" and any other string end with S end with S, can include any A and any N group group

.

The following reserved words can be used in a regular expression

^

The resulting string begins with the string of strings.

MySQL> SELECT "fonfo" regexp "^ fo $"; -> 0 (indicating mismatch)

MySQL> SELECT "FOFO" regexp "^ fo"; -> 1 (indicating match)

$

The matched string ends with the front string

MySQL> SELECT "fono" regexp "^ fono $"; -> 1 (indicating match)

MySQL> SELECT "fono" regexp "^ fo $"; -> 0 (indicating mismatch)

.

Match any character (including new line)

MySQL> SELECT "FOFO" regexp "^ f. *"; -> 1 (indicating match)

MySQL> SELECT "fonfo" regexp "^ f. *"; -> 1 (indicating match)

a *

Match any multiple A (including empty strings)

MySQL> SELECT "BAN" regexp "^ ba * n"; -> 1 (indicating match)

MySQL> SELECT "baaan" regexp "^ ba * n"; -> 1 (indicating match)

MySQL> SELECT "BN" regexp "^ ba * n"; -> 1 (indicating match)

A

Match any plurality A (excluding empty strings)

MySQL> SELECT "BAN" regexp "^ ba n"; -> 1 (indicating match)

MySQL> SELECT "BN" regexp "^ ba n"; -> 0 (indicating mismatch)

a?

Match one or zero A

MySQL> SELECT "BN" regexp "^ ba? n"; -> 1 (indicating match)

MySQL> SELECT "BAN" regexp "^ ba? n"; -> 1 (indicating match)

MySQL> SELECT "Baan" regexp "^ ba? n"; -> 0 (indicating mismatch) DE | ABC

Match DE or ABC

MySQL> SELECT "Pi" regexp "pi | APA"; -> 1 (indicating match)

MySQL> SELECT "AXE" regexp "pi | APA"; -> 0 (indicating mismatch)

MySQL> SELECT "APA" regexp "pi | APA"; -> 1 (indicating match)

MySQL> SELECT "APA" regexp "^ (PI | APA) $"; -> 1 (indicating match)

MySQL> SELECT "Pi" regexp "^ (PI | APA) $"; -> 1 (indicating match)

MySQL> SELECT "PIX" regexp "^ (PI | APA) $"; -> 0 (indicating that it does not match)

(ABC) *

Match any multiple ABCs (including empty strings)

MySQL> SELECT "Pi" regexp "^ (pi) * $"; -> 1 (indicating match)

MySQL> SELECT "PIP" regexp "^ (pi) * $"; -> 0 (indicating that it does not match)

MySQL> SELECT "PIPI" regexp "^ (pi) * $"; -> 1 (indicating match)

{1}

{2,3}

This is a more comprehensive method that enables several frontal features of several reserved words.

a *

Can be written into a {0,}

A

Can be written as a {1,}

a?

Can be written into a {0, 1}

There is only one integer parameter i in {}, indicating that characters can only appear I; there is an integer parameter i in {}.

The back is followed by ",", "indicates that the character can appear I or more or more; only one integer parameter i in {},

The back is followed by ",", followed by an integer parameter J, indicating that the characters can only appear above, J times

(Including I and J times). The integer parameters must be greater than or equal to 0, less than or equal to RE_DUP_MAX (default is 25

5).

If there are two parameters, the second must be greater than or equal to the first one

[a-dx]

Match "A", "B", "C", "D" or "X"

[^ a-dx]

Match any characters other than "A", "B", "C", "D", "X".

"[", "]" Must be used

MySQL> SELECT "AXBC" regexp "[A-DXYZ]"; -> 1 (indicating match)

MySQL> SELECT "AXBC" regexp "^ [A-DXYZ] $"; -> 0 (indicating mismatch)

MySQL> SELECT "AXBC" regexp "^ [A-DXYZ] $"; -> 1 (indicating match)

MySQL> SELECT "AXBC" regexp "^ [^ a-DXYZ] $"; -> 0 (indicating mismatch) mysql> select "gheis" regexp "^ [^ a-DXYZ] $"; -> 1 ( Matching)

MySQL> SELECT "Gheisa" regexp "^ [^ a-DXYZ] $"; -> 0 (indicating that it does not match)

-------------------------------------------------- ------------

[[.Characters.]]]]]]

Indicates the order of the comparison element. The order sequence in parentheses is unique. However, in parentheses can contain wildcards,

So he can match more characters. For example: regular expression [[.ch.]] * C Match the top five characters of ChCHCC

.

[= character_class =]

Represents equal classes, can replace other equivalent elements in class, including itself. For example, if O and ( ) are

Members of a equal class, then [[= O =]], [[= ( ) =]] and [O ( )] are complete equivalent.

[: character_class:]

In parentheses, in [: and:] is the name of the character class, you can represent all characters belonging to this class.

The characters of the character class are: AlNum, Digit, Punct, Alpha, Graph, Space, Blank, Lower, Uppe

R, CNTRL, PRINT and XDIGIT

MySQL> Select "Justalnums" regexp "[[: alnum:]] "; -> 1 (indicating match)

MySQL> SELECT "!!" regexp "[[: alnum:]] "; -> 0 (indicating mismatch)

[[: <:]]

[[:>:]]

Match a word start and ending empty string, this word is not included in Alnum

The characters cannot be underscore.

MySQL> SELECT "A Word a" regexp "[[: <:]] word [[:>:]]"; -> 1 (indicating match)

MySQL> SELECT "a xword a" regexp "[[: <:]] word [[:>:]]"; -> 0 (indicating that it does not match)

MySQL> SELECT "Weeknights" Regexp "^ (knights | Nights) $"; -> 1

match)

转载请注明原文地址:https://www.9cbs.com/read-91191.html

New Post(0)