You have seen MySQL pattern matching with LIKE ...%. MySQL supports
another type of pattern matching operation based on regular expressions and the
REGEXP operator.
A D V E R T I S E M E N T
If you are aware of PHP or PERL then its very simple for
you to understand because this matching is very similar to those scripting
regular expressions.
Following is the table of pattern which can be used along with REGEXP
operator.
Pattern
What the pattern matches
^
Beginning of string
$
End of string
.
Any single character
[...]
Any character listed between the square brackets
[^...]
Any character not listed between the square brackets
p1|p2|p3
Alternation; matches any of the patterns p1, p2, or p3
*
Zero or more instances of preceding element
+
One or more instances of preceding element
{n}
n instances of preceding element
{m,n}
m through n instances of preceding element
Examples:
Now based on above table you can device various type of SQL queries to meet
your requirements. Here I'm listing few for your understanding. Consider we have
a table called person_tbl and its having a field called name:
Query to find all the names starting with 'st'
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';
Query to find all the names ending with 'ok'
mysql> SELECT name FROM person_tbl WHERE name REGEXP 'ok$';
Query to find all the names which contains 'mar'
mysql> SELECT name FROM person_tbl WHERE name REGEXP 'mar';
Query to find all the names starting with a vowel and ending with 'ok'
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
MySQL Regexps, Mysql Tutorial, Mysql tutorial pdf, history of mysql, basic mysql, syntax use in mysql, mysql software download, learn mysql, mysql insert, mysql delete, mysql data types, mysql administrator.