mysql regexp usage

The role of regular expressions is to match formulas, comparing a pattern (regular expression) with a text string.

MySQL provides preliminary support for regular expressions with the WHERE clause, allowing you to specify regular expressions to filter the data retrieved by SELECT.

MySQL only supports a small subset of most regular expression implementations.

----------------------
1. The basic character matching

REGEXP is processed as a regular expression.

SELECT prod_name
FROM products
WHERE prod_name REGEXP '1000'
ORDER BY prod_name;

------return------
+------------------------+
|    prod_name    |
+------------------------+
|  JetPack 1000  |
+------------------------+


. means match any character.

SELECT prod_name
FROM products
WHERE prod_name REGEXP '.000'
ORDER BY prod_name;

------------return-----------

+-------------------------+
|     prod_name    |
+-------------------------+
|   JetPack 1000  |
|   JetPack 2000  |
+-------------------------+


Regular expression matching in MySQL is not case sensitive.

For case sensitivity, use the BINARY keyword.

Such as: WHERE prod_name REGEXP BINARY 'JetPack .000'

2. To search for one of the two strings (or this string, or another string), use | .

| As an OR operator, it means to match one of them. More than two OR conditions can be given.

SELECT prod_name
FROM products
WHERE prod_name REGEXP '1000 | 2000'
ORDER BY prod_name;

------------return------------

+----------------------+
|  prod_name   |
+----------------------+
| JetPack 1000 |
| JetPack 2000 |
+----------------------+



[ ] matches any single character.

[123] defines a set of characters that means match 1 or 2 or 3.

[ ] is another form of OR statement, and [123] Ton is short for [1 | 2 | 3] Ton.

^ negates a set of characters that will match anything but the specified character. [^123] will match anything but these characters.

SELECT prod_name
FROM products
WHERE prod_name REGEXP '[123] Ton'
ORDER BY prod_name;

-------------return------------

+--------------------+
| prod_name   |
+--------------------+
| 1 ton anvil    |
| 2 ton anvil    |
+--------------------+


Match range

[0123456789] or [0-9] will match digits 0 to 9

[az] match any alphabetic symbol

SELECT prod_name
FROM products
WHERE prod_name REGEXP '[1-5] Ton'
ORDER BY prod_name;

----------return-----------

+-------------------+
|  prod_name |
+-------------------+
|  .5 ton anvil  |
|  1 ton anvil   |
|  2 ton anvil   |
+-------------------+


Match the special character

\\ as leading. i.e. escaping. All characters that have special meaning within a regular expression must be escaped this way.

\\- means find -

\\. means find.

SELECT prod_name
FROM vendors
WHERE vend_name REGEXP '\\.'
ORDER BY vend_name;

-------------return-------------

+----------------------+
|  vend_name   |
+----------------------+
|  Furball Inc.    |
+----------------------+


\ is also used to quote metacharacters (characters with special meaning)
\\f form feed
\n newline
\\r Enter
\t tab
\\v vertical tab


match character class
code

[:a;num:] any letters and numbers (same as [a-zA-Z0-9])
[:alpha:] any character (same as [a-zA-Z])
[:blank:] spaces and tabs (same as [\\t])
[:cntrl:] ASCII control characters (ASCII 0 to 31 and 127)
[:digit:] Any digit (same as [0-9])
[:graph:] is the same as ["print:], but without spaces
[:lower:] Any lowercase word line (same as [az])
[:print:] any printable character
[:punct:] any character that is neither in [:alnum:] nor in [:cntrl:]
[space:] any whitespace character including spaces (same as [\\f\\n\\t\\r\\v])
[:upper:] letters of any size (same as [AZ])
[:xdigit:] any hexadecimal digit (same as [a-fA-F0-9])


match multiple instances
Metacharacter description
* 0 or more matches
+ 1 or more matches (equal to {1, })
? 0 or 1 matches (equal to {0, 1})
{n} specifies the number of matches
{n, } no less than specified number of matches
{n ,m} The range of matching numbers (m does not exceed 255)


The following example: ? after s makes s optional because ? matches 0 or 1 occurrence of any character preceding it.
SELECT prod_name
FROM products
WHERE prod_name REGEXP '\\([0-9] sticks?\\)'
ORDER BY prod_name;

------------return------------

+-----------------------+
|  prod_name    |
+-----------------------+
|  TNT (1 stick)   |
|  TNT (5 sticks) |
+-----------------------+


Double use of ^: in a set (defined with [ ]), use it to negate the set. Otherwise, used to refer to the beginning of the string and.

LIKE matches the entire string, while REGEXP matches substrings.

    Simple Regular Expression Testing Regular expressions can be tested with SELECT without using a database.

    REGEXP checks always return 0 (no match) or 1 (match), you can use REGEXP with literal strings to

    test expressions, and experiment with them. The corresponding syntax is as follows:

        SELECT 'hello' REGEXP '[0-9]'

    This example returns 0 (because there are no numbers in the text hello).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326702590&siteId=291194637