MySQL

REGEXP Matching Ranges

Sets can be used to define one or more characters to be matched. For example, the following will match digits 0 through 9:

[0123456789]

To simplify this type of set, - can be used to define a range. The following is functionally identical to the list of digits just seen:

[0-9]

Ranges are not limited to complete sets[1-3] and [6-9] are valid ranges, too. In addition, ranges need not be numeric, and so [a-z] will match any alphabetical character.

Here is an example:

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

+------------+
| prod_name  |
+------------+
| 1 product  |
| 2 product  |
| 3 product  |
| 4 product  |
| 5 product  |
+------------+

Here the regular expression [1-5] product was used. [1-5] defines a range, and so this expression means match 1 through 5, and so five matches were returned.

by BrainBellupdated
Advertisement: