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
acharacters.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
acharacters.mysql>
SELECT 'Ban' REGEXP '^Ba+n';-> 1
mysql>SELECT 'Bn' REGEXP '^Ba+n';-> 0 -
a?Match either zero or one
acharacter.mysql>
SELECT 'Bn' REGEXP '^Ba?n';-> 1
mysql>SELECT 'Ban' REGEXP '^Ba?n';-> 1
mysql>SELECT 'Baan' REGEXP '^Ba?n';-> 0 -
de|abcMatch either of the sequences
deorabc.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.mandnare 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 exactlyninstances ofa.a{n,}matchesnor more instances ofa.a{m,n}matchesmthroughninstances ofa, inclusive.mandnmust be in the range from0toRE_DUP_MAX(default 255), inclusive. If bothmandnare given,mmust 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,dorX. 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