MySQL must know will be - to use regular expressions to search

With regular expression search

Use MySQL Regular Expressions

The basic character matches

The following statement retrieves all lines of text columns prod_name 1000:

Entry

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

Export

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

analysis

The keyword LIKE REGEXP be replaced, but this statement looks very like LIKE statement. It tells MySQL: REGEXP after being told things as a regular expression (matching the text with the text of a 1000 match regular expression) process.

In the above example, the regular expression does not bring much benefit (may also degrade performance), but please consider the following example:

Entry

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

Export

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

analysis

This uses regular expressions **. 000. ** .is a regular expression language is a special character. It means match any one character.

Of course, this particular example may be used to complete LIKE and wildcards.

Matching case-insensitive in MySQL regular expression matching (since version 3.23.4) are not case sensitive (that is, uppercase and lowercase match). Case-sensitive, can be used BINARY keyword, such as WHERE prod_name REGEXP BINARY 'JetPack .000' .

An OR match

Search one of the two strings (or of the string, a string or another), the use |, as follows:

Entry

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

Export

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

analysis

Statement uses regular expressions 1000 | 2000. |Positive expression OR operator. It means match one of them.

OR two or more conditions may be given more than two OR condition. For example, '1000 | 2000 | 3000' to 1000 or 2000 or 3000 match.

Match one of several characters

Matches any single character. By specifying a set of use [and ]to complete the enclosed character, as follows:

Entry

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

Export

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

analysis

[123] defines a set of characters, it means 1 or 2 or 3 matching.

As can be seen, [] it is another form of the OR statement. In fact, the regular expression [123] Ton of [1 | 2 | 3] Ton Abbreviation, the latter may also be used. However, the need to use [] to define what OR statement finds. To better understand this, consider the following example:

Entry

SELECT prod_name
FROM products
WHERE prod_name REGEXP '1|2|3 Ton'
ORDER BY prod_name;

Export

+---------------+
| prod_name     |
+---------------+
| 1 ton anvil   |
| 2 ton anvil   |
| JetPack 1000  |
| JetPack 2000  |
| TNT (1 stick) |
+---------------+

analysis

This is not the desired output. It requires two rows are retrieved, but also an additional 3 rows retrieved. The reason for this is because MySQL assume you mean '1' or '2' or '3 Ton'. Unless the characters |included in a collection, or it applies to the entire series.

Character set can also be negative, that is, they will match anything except the specified character. A negative character set, at the beginning of the collection of placing a ^can. Thus, although the [123] matches the character 1, 2 or 3, but [^123]they match anything in addition to the characters.

Range matching

Used to define a set of one or more characters to be matched. For example, the following set of matching numbers 0 to 9:

[0123456789]

To simplify the collection of this type may be used -to define a range. The following equation is functionally identical to the digital list:

[0-9]

Range is not limited to the complete set. In addition, not necessarily just the range of values, [AZ] matches any alphabetic characters.

for instance:

Entry

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

Export

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

analysis

As used herein, the regular expression [1-5] Ton. [1-5] a defined range, the expression for the match 1-5, the flow returns three matching line. Since 5 ton match is returned. 5 ton.

Matching special characters

Entry

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

Export

+----------------+
| vend_name      |
+----------------+
| ACME           |
| Anvils R Us    |
| Furball Inc.   |
| Jet Set        |
| Jouets Et Ours |
| LT Supplies    |
+----------------+

analysis

This is not the desired output, .matches any character, so each row to be retrieved.

To match the special characters, it must be \\for the preamble. \\-A lookup -, \\.means the search ..

Entry

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

Export

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

analysis

This is the desired output. \\.Match ., so only one row retrieved. All the characters in this process is the so-called escape (escaping), regular expressions have special significance within must be escaped in this manner.

\\Also used to refer to meta characters (characters have special meanings), are listed below.

Metacharacters Explanation
\\f Feed
\\n Wrap
\\r Enter
\\t tabulation
\\ v Vertical tab

Matching \ In order to match the backslash \character itself, you need to use \\\.

\ Or \\? Most regular expression implementations use a single backslash escape special characters in order to use these characters themselves. But MySQL requires two backslash (MySQL their own interpretation of a regular expression library to explain another).

Matching character class

It is more convenient to work with predefined set of characters, called the character class (character class). The following table lists the character classes and what they mean.

class Explanation
[: Scooping] Any letters and numbers (with [a-zA-Z0-9])
[:alpha:] Arbitrary character (with [a-zA-Z])
[:blank:] Spaces and tabs (with [\ T])
[:cntrl:] ASCII control characters (ASCII 0 to 31 and 127)
[:digit:] Any number (with the [0-9])
[:graph:] And [: print:] the same, but does not include spaces
[:lower:] Any lowercase letter (with [AZ])
[:print:] Any printable character
[:point:] Neither [: alnum:] not in [: cntrl:] is any character
[:space:] Any blank characters including spaces (with [\\ f \\ n \\ r \\ t \\ v])
[: Unnper:] Any capital letters (with [AZ])
[: Xdigit] Any hexadecimal digit (with [a-fA-F0-9])

Examples of the plurality of matching

Repetition Metacharacters

Metacharacters Explanation
* Zero or more matches
+ One or more matches (is equal to {1})
? 0 or 1 matches (is equal to {0,1})
{n} A specified number of matches
{n, } Less than a specified number of matches
{n, m} Number of matching range (m 255)

Entry

SELECT prod_name
FROM products
WHERE prod_name REGEXP '\\([0-9] sticks?\\)'
ORDER BY prod_name;

Export

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

analysis

\\(Match (, [0-9] to match any number, Sticks? Matching and stick Sticks (after s? S so Alternatively, since? 0 or 1 appears before it matches any character), \\)match ).

Entry

SELECT prod_name
FROM products
WHERE prod_name REGEXP '[[:digit:]]{4}'
ORDER BY prod_name;

Export

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

analysis

[: Digit:] to match any number, since it is a set number. {4} exactly its preceding claim character (an arbitrary number) occurs four times, so that [[: digit:]] {4} matching linked to any four-digit number.

The above example can also be written as follows:

SELECT prod_name
FROM products
WHERE prod_name REGEXP '[0-9][0-9][0-9][0-9]'
ORDER BY prod_name;

Locator

To match a particular location of text, the following table lists the required locator.

Metacharacters Explanation
^ The beginning of the text
$ End of text
[[:<:]] Beginning of a word
[[:>:]] End of a word

Entry

SELECT prod_name
FROM products
WHERE prod_name REGEXP '^[0-9\\.]'
ORDER BY prod_name;

Export

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

analysis

^Matches the beginning of string. Thus, ^[0-9\\.]only .they match when the first character string or any number of.

^ Dual-use ^ in two ways. In the collection (by [and] defined), using it to set the negative, otherwise, used to refer to at the beginning of the string.

So that acts similar to LIKE REGEXP LIKE and REGEXP except that, while the entire string matching REGEXP LIKE matching substring. Using a locator, by starting with each expression ^, $ end with each expression may be acting as REGEXP and LIKE.

Simple regular expression test can be used to test regular expressions SELECT without the use of a database table. Check REGEXP always returns 0 (no match) or 1 (match), can be used with the character string REGEXP to test expression, and tested for their corresponding syntax is as follows:

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

This example obviously will return 0.

Published 32 original articles · won praise 18 · views 3241

Guess you like

Origin blog.csdn.net/u011714517/article/details/104088450