MySQL Tutorial/Regular Expressions/Occurrences

Материал из SQL эксперт
Версия от 09:54, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

{n} or {m,n} notation provides a more general way of writing regular expressions: {1}, {2,3}

{n} or {m,n} notation matches many occurrences of the previous atom (or "piece") of the pattern.

m and n are integers.

  1. a*: Can be written as a{0,}.
  2. a+: Can be written as a{1,}.
  3. a?: Can be written as a{0,1}.
  4. a{n} matches exactly n instances of a.
  5. a{n,} matches n or more instances of a.
  6. a{m,n} matches m through n instances of a, inclusive.

m and n must be in the range from 0 to RE_DUP_MAX (default 255), inclusive.

If both m and n are given, m must be less than or equal to n.



mysql>
mysql> SELECT "abcde" REGEXP "a[bcd]{2}e";
+-----------------------------+
| "abcde" REGEXP "a[bcd]{2}e" |
+-----------------------------+
|                           0 |
+-----------------------------+
1 row in set (0.01 sec)


SELECT "abcde" REGEXP "a[bcd]{1,10}e";

mysql>
mysql> SELECT "abcde" REGEXP "a[bcd]{1,10}e";
+--------------------------------+
| "abcde" REGEXP "a[bcd]{1,10}e" |
+--------------------------------+
|                              1 |
+--------------------------------+
1 row in set (0.00 sec)
mysql>


SELECT "abcde" REGEXP "a[bcd]{3}e";

mysql>
mysql>
mysql> SELECT "abcde" REGEXP "a[bcd]{3}e";
+-----------------------------+
| "abcde" REGEXP "a[bcd]{3}e" |
+-----------------------------+
|                           1 |
+-----------------------------+
1 row in set (0.02 sec)