A regular expression describes a set of strings. The simplest regular expression is one that has no special characters in it. For example, the regular expression hello
matches hello
and nothing else.
Nontrivial regular expressions use certain special constructs so that they can match more than one string. For example, the regular expression hello|word
matches either the string hello
or the string word
.
As a more complex example, the regular expression B[an]*s
matches any of the strings Bananas
, Baaaaas
, Bs
, and any other string starting with a B
, ending with an s
, and containing any number of a
or n
characters in between.
A regular expression for the REGEXP
operator may use any of the following special characters and constructs:
-
^
Match the beginning of a string.
mysql>
SELECT 'fo\nfo' REGEXP '^fo$';
-> 0
mysql>SELECT 'fofo' REGEXP '^fo';
-> 1
-
$
Match the end of a string.
mysql>
SELECT 'fo\no' REGEXP '^fo\no$';
-> 1
mysql>SELECT 'fo\no' REGEXP '^fo$';
-> 0 -
.
Match any character (including carriage return and newline).
mysql>
SELECT 'fofo' REGEXP '^f.*$';
-> 1
mysql>SELECT 'fo\r\nfo' REGEXP '^f.*$';
-> 1 -
a*
Match any sequence of zero or more
a
characters.mysql>
SELECT 'Ban' REGEXP '^Ba*n';
-> 1
mysql>SELECT 'Baaan' REGEXP '^Ba*n';
-> 1
mysql>SELECT 'Bn' REGEXP '^Ba*n';
-> 1 -
a+
Match any sequence of one or more
a
characters.mysql>
SELECT 'Ban' REGEXP '^Ba+n';
-> 1
mysql>SELECT 'Bn' REGEXP '^Ba+n';
-> 0 -
a?
Match either zero or one
a
character.mysql>
SELECT 'Bn' REGEXP '^Ba?n';
-> 1
mysql>SELECT 'Ban' REGEXP '^Ba?n';
-> 1
mysql>SELECT 'Baan' REGEXP '^Ba?n';
-> 0 -
de|abc
Match either of the sequences
de
orabc
.mysql>
SELECT 'pi' REGEXP 'pi|apa';
-> 1
mysql>SELECT 'axe' REGEXP 'pi|apa';
-> 0
mysql>SELECT 'apa' REGEXP 'pi|apa';
-> 1
mysql>SELECT 'apa' REGEXP '^(pi|apa)$';
-> 1
mysql>SELECT 'pi' REGEXP '^(pi|apa)$';
-> 1
mysql>SELECT 'pix' REGEXP '^(pi|apa)$';
-> 0 -
(abc)*
Match zero or more instances of the sequence
abc
.mysql>
SELECT 'pi' REGEXP '^(pi)*$';
-> 1
mysql>SELECT 'pip' REGEXP '^(pi)*$';
-> 0
mysql>SELECT 'pipi' REGEXP '^(pi)*$';
-> 1 -
{1}
,{2,3}
{n}
or{m,n}
notation provides a more general way of writing regular expressions that match many occurrences of the previous atom (or “piece”) of the pattern.m
andn
are integers.-
a*
Can be written as
a{0,}
. -
a+
Can be written as
a{1,}
. -
a?
Can be written as
a{0,1}
.
To be more precise,
a{n}
matches exactlyn
instances ofa
.a{n,}
matchesn
or more instances ofa
.a{m,n}
matchesm
throughn
instances ofa
, inclusive.m
andn
must be in the range from0
toRE_DUP_MAX
(default 255), inclusive. If bothm
andn
are given,m
must be less than or equal ton
.mysql>
SELECT 'abcde' REGEXP 'a[bcd]{2}e';
-> 0
mysql>SELECT 'abcde' REGEXP 'a[bcd]{3}e';
-> 1
mysql>SELECT 'abcde' REGEXP 'a[bcd]{1,10}e';
-> 1 -
-
[a-dX]
,[^a-dX]
Matches any character that is (or is not, if ^ is used) either
a
,b
,c
,d
orX
. A-
character between two other characters forms a range that matches all characters from the first character to the second. For example,[0-9]
matches any decimal digit. To include a literal]
character, it must immediately follow the opening bracket[
. To include a literal-
character, it must be written first or last. Any character that does not have a defined special meaning inside a[]
pair matches only itself.mysql>
SELECT 'aXbc' REGEXP '[a-dXYZ]';
-> 1
mysql>SELECT 'aXbc' REGEXP '^[a-dXYZ]$';
-> 0
mysql>SELECT 'aXbc' REGEXP '^[a-dXYZ]+$';
-> 1
mysql>SELECT 'aXbc' REGEXP '^[^a-dXYZ]+$';
-> 0
mysql>SELECT 'gheis' REGEXP '^[^a-dXYZ]+$';
-> 1
mysql>SELECT 'gheisa' REGEXP '^[^a-dXYZ]+$';
-> 0
No comments:
Post a Comment