Rechercher dans le manuel MySQL
3.3.4.7 Pattern Matching
MySQL provides standard SQL pattern matching as well as a form of pattern matching based on extended regular expressions similar to those used by Unix utilities such as vi, grep, and sed.
SQL pattern matching enables you to use _
to match any single character and %
to
match an arbitrary number of characters (including zero
characters). In MySQL, SQL patterns are case-insensitive by
default. Some examples are shown here. Do not use
=
or <>
when you
use SQL patterns. Use the LIKE
or
NOT LIKE
comparison operators
instead.
To find names beginning with b
:
- +--------+--------+---------+------+------------+------------+
- | name | owner | species | sex | birth | death |
- +--------+--------+---------+------+------------+------------+
- | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
- +--------+--------+---------+------+------------+------------+
To find names ending with fy
:
- +--------+--------+---------+------+------------+-------+
- | name | owner | species | sex | birth | death |
- +--------+--------+---------+------+------------+-------+
- +--------+--------+---------+------+------------+-------+
To find names containing a w
:
- +----------+-------+---------+------+------------+------------+
- | name | owner | species | sex | birth | death |
- +----------+-------+---------+------+------------+------------+
- | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
- +----------+-------+---------+------+------------+------------+
To find names containing exactly five characters, use five
instances of the _
pattern character:
- +-------+--------+---------+------+------------+-------+
- | name | owner | species | sex | birth | death |
- +-------+--------+---------+------+------------+-------+
- +-------+--------+---------+------+------------+-------+
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
REGEXP_LIKE()
function (or the
REGEXP
or
RLIKE
operators, which are synonyms for
REGEXP_LIKE()
).
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]
matchesa
,b
, orc
. 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 ofx
characters,[0-9]*
matches any number of digits, and.*
matches any number of anything.A regular expression pattern match succeeds if the pattern matches anywhere in the value being tested. (This differs from a
LIKE
pattern match, which succeeds only if the pattern matches the 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
LIKE
queries shown previously are
rewritten here to use
REGEXP_LIKE()
.
To find names beginning with b
, use
^
to match the beginning of the name:
- +--------+--------+---------+------+------------+------------+
- | name | owner | species | sex | birth | death |
- +--------+--------+---------+------+------------+------------+
- | Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
- +--------+--------+---------+------+------------+------------+
To force a regular expression comparison to be case sensitive,
use a case-sensitive collation, or use the
BINARY
keyword to make one of the
strings a binary string, or specify the c
match-control character. Each of these queries matches only
lowercase b
at the beginning of a name:
To find names ending with fy
, use
$
to match the end of the name:
- +--------+--------+---------+------+------------+-------+
- | name | owner | species | sex | birth | death |
- +--------+--------+---------+------+------------+-------+
- +--------+--------+---------+------+------------+-------+
To find names containing a w
, use this
query:
- +----------+-------+---------+------+------------+------------+
- | name | owner | species | sex | birth | death |
- +----------+-------+---------+------+------------+------------+
- | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
- +----------+-------+---------+------+------------+------------+
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 as would be true with an SQL pattern.
To find names containing exactly five characters, use
^
and $
to match the
beginning and end of the name, and five instances of
.
in between:
- +-------+--------+---------+------+------------+-------+
- | name | owner | species | sex | birth | death |
- +-------+--------+---------+------+------------+-------+
- +-------+--------+---------+------+------------+-------+
You could also write the previous query using the
{
(“repeat-n
}n
-times”)
operator:
- +-------+--------+---------+------+------------+-------+
- | name | owner | species | sex | birth | death |
- +-------+--------+---------+------+------------+-------+
- +-------+--------+---------+------+------------+-------+
For more information about the syntax for regular expressions, see Section 12.5.2, “Regular Expressions”.
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-pattern-matching.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.