Database Reference
In-Depth Information
▼
Output
+--------------+
| prod_name |
+--------------+
| JetPack 1000 |
| JetPack 2000 |
+--------------+
▼
Analysis
As explained previously,
[:digit:]
matches any digit, and so
[[:digit:]]
is a set of digits.
{4}
requires exactly four occurrences of whatever it follows
(any digit), and so
[[:digit:]]{4}
matches any four consecutive digits.
It is worth noting that when using regular expressions there is almost always
more than one way to write a specific expression. The previous example could
have also been written as follows:
▼
Input
SELECT prod_name
FROM products
WHERE prod_name REGEXP '[0-9][0-9][0-9][0-9]'
ORDER BY prod_name;
Actually, it could also have been written as
▼
Input
SELECT prod_name
FROM products
WHERE prod_name REGEXP '[0-9]{4}'
ORDER BY prod_name;
All the examples thus far have matched text anywhere within a string. To
match text at specific locations, you need to use anchors as listed in Table 9.4.
Table 9.4
Anchor Metacharacters
Metacharacter
Description
^
Start of text
$
End of text
[[:<:]]
Start of word
[[:>:]]
End of word