
MySQL provides a powerful way to perform complex text-based searches through the regular expressions function REGEXP. This string matching algorithm expresses a search through a sequence of symbols and characters to find a pattern in a text.
You can use regular expressions to match, locate, and manage text-based records in a MySQL database, saving a lot of time when working with complex searches in a huge database.
Apart from searching, you can also use REGEXP with a combination of other MySQL functions to replace and rearrange fields in a database table. You can also extract substrings from long text, split a string into human-readable tokens and verify/validate the structure of a string to check if it meets your set format.
This guide illustrates the power of the MySQL REGEXP function using examples. After completing it, you'll be able to implement different combinations of regular expressions in any MySQL database to achieve your desired results.
To complete this tutorial, you need the following:
sudo privileges.SSH to your server and log in to MySQL as root.
Key in your MySQL root password and press Enter to continue. Then, once you get the mysql> prompt, type the command below to create a test database.
Output.
Switch to the database.
Output.
Next, create a customers table.
Output.
Insert some records into the customers table.
Output.
Execute the SELECT statement below to verify the records.
You should see the following list of customers.
With the sample database and table in place, you'll now test the different regular expressions supported by MySQL.
To match a record beginning with a certain pattern, use the Caret character ^. For instance, to match all first_name's that start with the letter J, execute the command below.
You should see a list of customers matching your pattern, as shown below.
While this is for demonstration purposes, you can use the above example in a real-life scenario. For instance, if you're in the manufacturing industry, you can use this approach to select all batch numbers that begin with certain characters from a database to recall defective items.
In the next step, you'll see how you can match the end of a string.
Use the Dollar $ character to match the position of a string right after the last character. For instance, to match all last_name's ending with the letters OE, run the statement below.
Only MARY ROE's AND JOHN DOE's names end with the letters OE and they should be listed as follows.
You can use this approach, for instance, to retrieve all customers' records from a specific zip code if their address information is stored in a single column.
To test this, add address and zip columns to the customers table.
Output.
Next, update the customer's address information.
Output.
Make sure the table is updated.
Output
As you can see, the address details, including the street, town, state, and zip, are held in one column. For example, to get all customers from the zip code 8659, execute the command below.
Output.
You can also use MySQL REGEXP results in an UPDATE statement to edit records. For instance, in this case, you can now update the customer's zip code column to the correct value using the statement below.
Output.
Confirm the new changes.
Output
As you can see, the MySQL REGEXP function is essential when it comes to finding and rearranging text-based column values. In the next step, you'll search for values that match characters enclosed with square brackets.
You can match any character listed between the square brackets in MySQL using the regular expression. For instance, to list all first_name's containing the character H or Y, use the syntax below.
Output.
Similarly, use the command below to locate all last_name's that contain either the characters R or D followed by OE.
Output.
The two searches above are helpful when you're not sure about the exact name you're looking for in a database, but you at least happen to know some of the characters it contains. Next, you'll search a range of characters.
You can use a dash between a range of characters to locate a string using REGEXP. For instance, to retrieve all first_name's containing letters a-d, use the syntax below.
Output.
To match any zip codes that contain numbers 0-9, run the SQL command below.
Output.
In a production environment, you can use the above approaches to validate records. For instance, if you'd like to send text messages to your customers, you can filter records that only contain numeric phone numbers. Next, you'll match substrings.
You can match a substring at any position in MySQL using regular expressions. For instance, find all names that contain the substring AN in that order by running the query below.
Output.
Next, find a substring at a specific position by using the syntax below.
Output.
In most cases, you'll use substring to locate and replace erroneously entered data in MySQL. Next, you'll see how to use the character classes.
The MySQL REGEXP clause supports searching by character classes. This means you can search and return records that contain either alphabetical or numerical characters or even both.
For instance, use the syntax below to look for alphanumeric characters in the first_name field.
Since all first_name's contain alphanumeric characters, they should be listed as follows.
To search the alphabetical characters only, use the syntax below.
Output.
To search digits only, for instance, to locate zip codes with numeric characters, run the SQL command below.
Output.
Although this is not a definitive list of all MySQL regular expressions, it should help you grasp the basic concepts.
In this guide, you've created a sample database and a table. You've also tested the MySQL REGEXP function using different combinations. Use the knowledge in this guide when you want to perform complex searches in your next MySQL project.
0 Comments
Be the first to comment and share your perspective with the community.