
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What is MySQL REGEXP operator and how it handles pattern matching?
MySQL supports another type of pattern matching operation based on the regular expressions and the REGEXP operator. Following is the table of pattern, which can be used along with the REGEXP operator to handle the pattern matching.
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 |
Example
To illustrate the use of REGEXP we are using the table ‘Student_info’ having the following data −
mysql> Select * from Student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | +------+---------+------------+------------+ 5 rows in set (0.00 sec)
Now, followings are some queries that use the REGEXP to find the patterns of ‘Name’ from the above table −
mysql> Select Name from student_info WHERE Name REGEXP '^Y'; +---------+ | Name | +---------+ | YashPal | +---------+ 1 row in set (0.11 sec)
The above query will find all the names starting with ‘Y’.
mysql> Select name from student_info WHERE Name REGEXP 'am$'; +-------+ | name | +-------+ | Ram | | Shyam | +-------+ 2 rows in set (0.00 sec)
The above query will find all the names ending with ‘am’.
mysql> Select name from student_info WHERE Name REGEXP 'av'; +--------+ | name | +--------+ | Gaurav | +--------+ 1 row in set (0.00 sec)
The above query will find all the names containing ‘av’.
mysql> Select name from student_info WHERE Name REGEXP '^[aeiou]|am$'; +-------+ | name | +-------+ | Ram | | Shyam | +-------+ 2 rows in set (0.00 sec)
Above query will find all the names starting with a vowel and ending with ‘am’.
- Related Articles
- What is MySQL NULL-safe equal operator and how it is different from comparison operator?
- What is Pattern Matching in C# 7.0?
- Why BINARY keyword used with MySQL REGEXP operator?
- Matching using regexp in GoLang
- How to perform Multiline matching with JavaScript RegExp?
- How to perform Case Insensitive matching with JavaScript RegExp?
- Wildcard Pattern Matching
- What is Kicker Pattern and how does It work?
- MySQL pattern matching 3 or more “a's” in name?
- What is proxy design pattern and how to implement it in C#?
- What is the operator in MySQL?
- Unix filename pattern matching in Python
- Pattern matching in C# with Regex
- Pattern matching in Python with Regex
- Lua pattern matching vs regular expression

Advertisements