MySQL regular expressions

MySQL supports regular expression matching, and the REGEXP operator is used in MySQL for regular expression matching.

The regular patterns in the following table can be applied to the REGEXP operator.

Mode description
^ Matches the starting position of the input string.
$ Matches the end of the input string.
. Matches any single character except "\n". To match any character including '\n', use the pattern of '[.\n]'.
[...] character collection. Matches any one of the included characters. For example, '[abc]' can match 'a' in "plain".
[^...] Negative character set. Matches any character not included. For example, '[^abc]' matches the 'p' in "plain".
p1|p2|p3 Match p1 or p2 or p3. For example, 'z|food' matches "z" or "food". '(z|f)ood' matches "zood" or "food".
* Matches the preceding subexpression zero or more times. For example, zo* matches "z" as well as "zoo". * is equivalent to {0,}.
+ Matches the preceding subexpression one or more times. For example, 'zo+' matches "zo" and "zoo", but not "z". + is equivalent to {1,}.
{n} n is a non-negative integer. Matches a certain number of n times. For example, 'o{2}' would not match the 'o' in "Bob", but would match the two o's in "food".
{n,m} Both m and n are non-negative integers, where n <= m. Matches at least n times and at most m times.

example

After understanding the above rules of regular expressions, we can write SQL statements with regular expressions according to our own needs. The following will list a few small examples (table name: runoob_tbl  to deepen our understanding:

  1. MariaDB[RUNOOB]>select*from runoob_tbl;
  2. +-----------+---------------+---------------+-----------------+
  3. | runoob_id | runoob_title | runoob_author | submission_date |
  4. +-----------+---------------+---------------+-----------------+
  5. |1|Learn PHP |JohnPoul|2007-05-24|
  6. |2|LearnMySQL|Abdul S |2007-05-24|
  7. |3| JAVA Tutorial|Sanjay|2007-05-06|
  8. +-----------+---------------+---------------+-----------------+
  9. 3 rows inset(0.00 sec)
 

Find all data starting with 'Jo' in the runoob_author field:

MariaDB [RUNOOB]> SELECT * FROM runoob_tbl WHERE runoob_author REGEXP '^Jo';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
|         1 | Learn PHP    | John Poul     | 2007-05-24      |
+-----------+--------------+---------------+-----------------+
1 row in set (0.00 sec)

Find all data in the runoob_author field that ends with 'y':

MariaDB [RUNOOB]> SELECT * FROM runoob_tbl WHERE runoob_author REGEXP 'y$';
+-----------+---------------+---------------+-----------------+
| runoob_id | runoob_title  | runoob_author | submission_date |
+-----------+---------------+---------------+-----------------+
|         3 | JAVA Tutorial | Sanjay        | 2007-05-06      |
+-----------+---------------+---------------+-----------------+
1 row in set (0.00 sec)

查找runoob_author字段中包含'du'字符串的所有数据:

MariaDB [RUNOOB]> SELECT * FROM runoob_tbl WHERE runoob_author REGEXP 'du';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
|         2 | Learn MySQL  | Abdul S       | 2007-05-24      |
+-----------+--------------+---------------+-----------------+
1 row in set (0.00 sec)

查找runoob_author字段中以元音字符开头或以'ay'字符串结尾的所有数据:

MariaDB [RUNOOB]> SELECT * FROM runoob_tbl WHERE runoob_author REGEXP '^[aeiou]|ay$';
+-----------+---------------+---------------+-----------------+
| runoob_id | runoob_title  | runoob_author | submission_date |
+-----------+---------------+---------------+-----------------+
|         2 | Learn MySQL   | Abdul S       | 2007-05-24      |
|         3 | JAVA Tutorial | Sanjay        | 2007-05-06      |
+-----------+---------------+---------------+-----------------+
2 rows in set (0.00 sec)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326450535&siteId=291194637