..
The internal engine of MySQL has native - in addition to the common operators of equality, inequality and similarity (LIKE) - a very useful operator: REGEXP operator.
As its name suggests, the REGEXP operator leverages the power of regular expressions to perform complex searches within our MySQL database.
The syntax is simple:
SELECT field_name FROM table_name WHERE field_name REGEXP expression;Let's follow a simple summary table of the main characters and the special syntax used in the expression of comparison:
| ^ | Beginning of the string |
| $ | End of the string |
| . | Any character |
| [...] | Any of the characters in the brackets |
| [^...] | Any character except those in square brackets |
| | | Separate strings of characters or their alternatives |
| * | Zero or more repetitions of the previous character or the string |
| + | One or more repetitions of the previous character or the string |
| {N} | "N" character or the repetition of the previous string |
| {Min, max} | Repeats the previous character or string to a number of times between a minimum and a maximum |
1) We find all names starting with "m"
SELECT name FROM calendar WHERE name REGEXP '^ m';Our query will return, for example:
SELECT name FROM calendar WHERE name REGEXP 'or $';Our query will return, for example:
SELECT name FROM calendar WHERE name REGEXP '^ m + o $';Our query will return, for example:
SELECT name FROM calendar WHERE name REGEXP 'r';Our query will return, for example:
SELECT name FROM calendar WHERE name REGEXP 'ma | ra';Our query will return, for example:
SELECT name FROM calendar WHERE name REGEXP '[^ ma]';Our query will return, for example:
| |
MS Access (Advanced)
Learn how to create and manage databases quickly and easily. Starting from 29 €. |
| |
MySQL (Course)
Management of open-source database. From 39 €. |
| |
SQL and Database (Course)
Create and manage relational databases. From 39 €. |