16. Learn MySQL Regular Expressions

 MySQL regular expressions

In the previous chapters, we have learned that MySQL can   perform fuzzy matching through LIKE ...% .

MySQL also supports the matching of other regular expressions. The REGEXP operator is used in MySQL for regular expression matching.

If you know PHP or Perl, it's pretty straightforward, as MySQL's regular expression matching is similar to those of these scripts.

The regular patterns in the following table can be applied to the REGEXP operator.

example

After understanding the above regular requirements, we can write SQL statements with regular expressions according to our own needs. Below we will list a few small examples (table name: person_tbl ) to deepen our understanding:

Find all data starting with 'st' in the name field:

mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';

Find all data that ends with 'ok' in the name field:

mysql> SELECT name FROM person_tbl WHERE name REGEXP 'ok$';

Find all data containing the 'mar' string in the name field:

mysql> SELECT name FROM person_tbl WHERE name REGEXP 'mar';

Find all data in the name field that starts with a vowel character or ends with the string 'ok':

mysql> SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';

Guess you like

Origin blog.csdn.net/m0_66404702/article/details/127090020