Changes

Jump to: navigation, search

MySQL Regular Expression Searches

1,066 bytes added, 18:12, 19 October 2007
no edit summary
<td>[:xdigit:]<td>Any hexadecimal digit. Equivalent to [A-F], [a-f] and [0-9]
</table>
 
Let's now look at some examples. Suppose in our product database we have two products with similar names, the 'One&One VoIP Headset' and the "One2One USB Hub'. In order to retrieve the 'One2One' product we would use the [:digit:] character class:
 
<pre>
mysql> SELECT * FROM product WHERE prod_name REGEXP 'One[[:digit:]]One';
+-----------+-----------------+----------------+
| prod_code | prod_name | prod_desc |
+-----------+-----------------+----------------+
| 19 | One2One USB Hub | 4 Port USB Hub |
+-----------+-----------------+----------------+
1 row in set (0.00 sec)
</pre>
 
Similarly, to extract the 'One&One' product we would use the [:punct:] class:
 
<pre>
mysql> SELECT * FROM product WHERE prod_name REGEXP 'One[[:punct:]]One';
+-----------+----------------------+----------------+
| prod_code | prod_name | prod_desc |
+-----------+----------------------+----------------+
| 18 | One&One VoIP Headset | Stereo Headset |
+-----------+----------------------+----------------+
1 row in set (0.02 sec)
</pre>
 
 
== Regular Expression Repetition Metacharacters ==
In addition to allowing searches on single instances, regular expressions can also be written to look for repetition in text.

Navigation menu