Oracle 5.0 Reference Manual page 229

Table of Contents

Advertisement

The other type of pattern matching provided by MySQL uses extended regular expressions. When you
test for a match for this type of pattern, use the
[899]
and
RLIKE
NOT RLIKE
The following list describes some characteristics of extended regular expressions:
• "." matches any single character.
• A character class "[...]" matches any character within the brackets. For example, "[abc]"
matches "a", "b", or "c". To name a range of characters, use a dash. "[a-z]" matches any letter,
whereas "[0-9]" matches any digit.
• "*" matches zero or more instances of the thing preceding it. For example, "x*" matches any
number of "x" characters, "[0-9]*" matches any number of digits, and ".*" matches any number of
anything.
• A
[899]
pattern match succeeds if the pattern matches anywhere in the value being tested.
REGEXP
(This differs from a
LIKE
entire value.)
• To anchor a pattern so that it must match the beginning or end of the value being tested, use "^" at
the beginning or "$" at the end of the pattern.
To demonstrate how extended regular expressions work, the
are rewritten here to use
To find names beginning with "b", use "^" to match the beginning of the name:
mysql>
SELECT * FROM pet WHERE name REGEXP '^b';
+--------+--------+---------+------+------------+------------+
| name
| owner
| species | sex
+--------+--------+---------+------+------------+------------+
| Buffy
| Harold | dog
| Bowser | Diane
| dog
+--------+--------+---------+------+------------+------------+
If you really want to force a
keyword to make one of the strings a binary string. This query matches only lowercase "b" at the
beginning of a name:
mysql>
SELECT * FROM pet WHERE name REGEXP BINARY '^b';
To find names ending with "fy", use "$" to match the end of the name:
mysql>
SELECT * FROM pet WHERE name REGEXP 'fy$';
+--------+--------+---------+------+------------+-------+
| name
| owner
| species | sex
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat
| Buffy
| Harold | dog
+--------+--------+---------+------+------------+-------+
To find names containing a "w", use this query:
mysql>
SELECT * FROM pet WHERE name REGEXP 'w';
+----------+-------+---------+------+------------+------------+
| name
| owner | species | sex
+----------+-------+---------+------+------------+------------+
| Claws
| Gwen
| cat
| Bowser
| Diane | dog
| Whistler | Gwen
| bird
+----------+-------+---------+------+------------+------------+
Because a regular expression pattern matches if it occurs anywhere in the value, it is not necessary in
the previous query to put a wildcard on either side of the pattern to get it to match the entire value like it
would be if you used an SQL pattern.
Retrieving Information from a Table
REGEXP
[899], which are synonyms).
[896]
pattern match, which succeeds only if the pattern matches the
[899].
REGEXP
| birth
| f
| 1989-05-13 | NULL
| m
| 1989-08-31 | 1995-07-29 |
[899]
comparison to be case sensitive, use the
REGEXP
| birth
| f
| 1993-02-04 | NULL
| f
| 1989-05-13 | NULL
| birth
| m
| 1994-03-17 | NULL
| m
| 1989-08-31 | 1995-07-29 |
| NULL | 1997-12-09 | NULL
209
[899]
and
NOT REGEXP
[896]
queries shown previously
LIKE
| death
|
|
| death |
|
|
| death
|
|
|
[899]
operators (or
[948]
BINARY

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents