
- MySQL - Home
- MySQL - Introduction
- MySQL - Features
- MySQL - Versions
- MySQL - Variables
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Node.js Syntax
- MySQL - Java Syntax
- MySQL - Python Syntax
- MySQL - Connection
- MySQL - Workbench
- MySQL Databases
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Show Database
- MySQL - Copy Database
- MySQL - Database Export
- MySQL - Database Import
- MySQL - Database Info
- MySQL Users
- MySQL - Create Users
- MySQL - Drop Users
- MySQL - Show Users
- MySQL - Change Password
- MySQL - Grant Privileges
- MySQL - Show Privileges
- MySQL - Revoke Privileges
- MySQL - Lock User Account
- MySQL - Unlock User Account
- MySQL Tables
- MySQL - Create Tables
- MySQL - Show Tables
- MySQL - Alter Tables
- MySQL - Rename Tables
- MySQL - Clone Tables
- MySQL - Truncate Tables
- MySQL - Temporary Tables
- MySQL - Repair Tables
- MySQL - Describe Tables
- MySQL - Add/Delete Columns
- MySQL - Show Columns
- MySQL - Rename Columns
- MySQL - Table Locking
- MySQL - Drop Tables
- MySQL - Derived Tables
- MySQL Queries
- MySQL - Queries
- MySQL - Constraints
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Replace Query
- MySQL - Insert Ignore
- MySQL - Insert on Duplicate Key Update
- MySQL - Insert Into Select
- MySQL Indexes
- MySQL - Indexes
- MySQL - Create Index
- MySQL - Drop Index
- MySQL - Show Indexes
- MySQL - Unique Index
- MySQL - Clustered Index
- MySQL - Non-Clustered Index
- MySQL Operators and Clauses
- MySQL - Where Clause
- MySQL - Limit Clause
- MySQL - Distinct Clause
- MySQL - Order By Clause
- MySQL - Group By Clause
- MySQL - Having Clause
- MySQL - AND Operator
- MySQL - OR Operator
- MySQL - Like Operator
- MySQL - IN Operator
- MySQL - ANY Operator
- MySQL - EXISTS Operator
- MySQL - NOT Operator
- MySQL - NOT EQUAL Operator
- MySQL - IS NULL Operator
- MySQL - IS NOT NULL Operator
- MySQL - Between Operator
- MySQL - UNION Operator
- MySQL - UNION vs UNION ALL
- MySQL - MINUS Operator
- MySQL - INTERSECT Operator
- MySQL - INTERVAL Operator
- MySQL Joins
- MySQL - Using Joins
- MySQL - Inner Join
- MySQL - Left Join
- MySQL - Right Join
- MySQL - Cross Join
- MySQL - Full Join
- MySQL - Self Join
- MySQL - Delete Join
- MySQL - Update Join
- MySQL - Union vs Join
- MySQL Keys
- MySQL - Unique Key
- MySQL - Primary Key
- MySQL - Foreign Key
- MySQL - Composite Key
- MySQL - Alternate Key
- MySQL Triggers
- MySQL - Triggers
- MySQL - Create Trigger
- MySQL - Show Trigger
- MySQL - Drop Trigger
- MySQL - Before Insert Trigger
- MySQL - After Insert Trigger
- MySQL - Before Update Trigger
- MySQL - After Update Trigger
- MySQL - Before Delete Trigger
- MySQL - After Delete Trigger
- MySQL Data Types
- MySQL - Data Types
- MySQL - VARCHAR
- MySQL - BOOLEAN
- MySQL - ENUM
- MySQL - DECIMAL
- MySQL - INT
- MySQL - FLOAT
- MySQL - BIT
- MySQL - TINYINT
- MySQL - BLOB
- MySQL - SET
- MySQL Regular Expressions
- MySQL - Regular Expressions
- MySQL - RLIKE Operator
- MySQL - NOT LIKE Operator
- MySQL - NOT REGEXP Operator
- MySQL - regexp_instr() Function
- MySQL - regexp_like() Function
- MySQL - regexp_replace() Function
- MySQL - regexp_substr() Function
- MySQL Fulltext Search
- MySQL - Fulltext Search
- MySQL - Natural Language Fulltext Search
- MySQL - Boolean Fulltext Search
- MySQL - Query Expansion Fulltext Search
- MySQL - ngram Fulltext Parser
- MySQL Functions & Operators
- MySQL - Date and Time Functions
- MySQL - Arithmetic Operators
- MySQL - Numeric Functions
- MySQL - String Functions
- MySQL - Aggregate Functions
- MySQL Misc Concepts
- MySQL - NULL Values
- MySQL - Transactions
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - SubQuery
- MySQL - Comments
- MySQL - Check Constraints
- MySQL - Storage Engines
- MySQL - Export Table into CSV File
- MySQL - Import CSV File into Database
- MySQL - UUID
- MySQL - Common Table Expressions
- MySQL - On Delete Cascade
- MySQL - Upsert
- MySQL - Horizontal Partitioning
- MySQL - Vertical Partitioning
- MySQL - Cursor
- MySQL - Stored Functions
- MySQL - Signal
- MySQL - Resignal
- MySQL - Character Set
- MySQL - Collation
- MySQL - Wildcards
- MySQL - Alias
- MySQL - ROLLUP
- MySQL - Today Date
- MySQL - Literals
- MySQL - Stored Procedure
- MySQL - Explain
- MySQL - JSON
- MySQL - Standard Deviation
- MySQL - Find Duplicate Records
- MySQL - Delete Duplicate Records
- MySQL - Select Random Records
- MySQL - Show Processlist
- MySQL - Change Column Type
- MySQL - Reset Auto-Increment
- MySQL - Coalesce() Function
MySQL - REGEXP Operator
The MySQL REGEXP operator is used for pattern matching using regular expressions. It returns 1 if there is a match, and 0 if there is none. This operator is similar to the REGEXP_LIKE() function, but provides additional options such as case-insensitivity and Unicode character ranges.
A regular expression (also called as regex or regexp) is a sequence of characters that forms a search pattern. They can be used to perform all types of text search and text replace operations.
MySQL offers various functions and operators for working with regular expressions, including REGEXP, REGEXP_REPLACE, REGEXP_INSTR, REGEXP_SUBSTR, and REGEXP_LIKE. These functions allow you to search for patterns in strings, replace or modify text within strings, extract substrings that match a specified regular expression pattern, and determine whether a string matches a given regular expression pattern.
Syntax
Following is the syntax of MySQL REGEXP operator −
expr REGEXP pat
It has two operands −
expr − A string value (left) in which you want to carry on the search.
pat − A regular expression (right) for which you want to search.
Example
Let's start with a simple example where we check if the string "Welcome to Tutorialspoint" contains the pattern "to" −
SELECT 'Welcome to Tutorialspoint' REGEXP 'to';
Following is the output of the above code −
'Welcome to Tutorialspoint' REGEXP 'to' |
---|
1 |
Example
In this case, we're searching for the pattern "is" within the string "This is a sample string."−
SELECT 'This is a sample string' REGEXP 'is';
Output of the above code is as shown below −
'This is a sample string' REGEXP 'is' |
---|
1 |
Example
You can also use the RLIKE operator as an alternative to REGEXP. It provides the same result as REGEXP. Here, we check for the pattern "unknown" in the string "Welcome to Tutorialspoint."
If the given pattern is not found in the string, this operator returns 0 −
SELECT 'Welcome to Tutorialspoint' REGEXP 'unknown';
The output obtained is as follows −
'Welcome to Tutorialspoint' REGEXP 'unknown'; |
---|
0 |
Here is another example −
SELECT 'This is a sample string' RLIKE 'value';
The result produced is as follows −
'This is a sample string' RLIKE 'value'; |
---|
0 |
Example
If either of the first two operands is NULL, this operator returns NULL. Here, we check if NULL matches the pattern "value" −
SELECT NULL REGEXP 'value';
We get the output as follows −
NULL REGEXP 'value' |
---|
NULL |
Following is another example −
SELECT 'Tutorialspoint' REGEXP NULL;
Following is the output of the above code −
'Tutorialspoint' REGEXP NULL |
---|
NULL |
Example
You can use the NOT clause to reverse the results. It returns 0 in case of a match and 1 if there's no match.
In this example, we check if the pattern "is" is NOT present in the string "This is a sample string" −
SELECT NOT 'This is a sample string' REGEXP 'is';
Output of the above code is as shown below −
NOT 'This is a sample string' REGEXP 'is' |
---|
0 |
Following is another example −
SELECT NOT 'Welcome to Tutorialspoint' REGEXP 'unknown';
The result obtained is as follows −
NOT 'Welcome to Tutorialspoint' REGEXP 'unknown' |
---|
1 |
Example
Suppose we have created a table named "EMP_TABLE" in the database using the CREATE statement and inserted three records in it as shown below −
CREATE TABLE EMP_TABLE( ID VARCHAR(5), Name VARCHAR(255), DOB DATE, Location VARCHAR(255) );
Now, let us insert records into it using the INSERT statement −
INSERT INTO EMP_TABLE VALUES ('101', 'Amit', DATE('1970-01-08'), 'Hyderabad'), ('102','Sumith', DATE('1990-11-02'), 'Vizag'), ('105','Sudha', DATE('1980-11-06'), 'Delhi'), ('MyID2','Raja', DATE('1980-11-06'), 'Goa'), ('109','Javed', DATE('1980-11-06'), 'Pune'), ('AA', 'Suma', DATE('1980-11-06'), 'Chennai'), ('120','Vani', DATE('1980-11-06'), 'Delhi'), ('oo1','Devi', DATE('1980-11-06'), 'Goa');
Following query displays the records which have ID values containing only digits −
SELECT * FROM EMP_TABLE where ID REGEXP '^[0-9]{3}$';
After executing the above code, we get the following output −
ID | NAME | DOB | Location |
---|---|---|---|
101 | Amit | 1970-01-08 | Hyderabad |
102 | Sumith | 1990-11-02 | Vizag |
105 | Sudha | 1980-11-06 | Delhi |
109 | Javed | 1980-11-06 | Pune |
120 | Vani | 1980-11-06 | Delhi |
Following query displays the records of EMP_TABLE whose name start with the letter "s" −
SELECT * FROM EMP_TABLE where Name REGEXP '^[s]';
The result produced is as shown below −
ID | NAME | DOB | Location |
---|---|---|---|
102 | Sumith | 1990-11-02 | Vizag |
105 | Sudha | 1980-11-06 | Delhi |
AA | Suma | 1980-11-06 | Chennai |
Following query displays all the records whose name contains either "Raja" or "Roja" −
SELECT * FROM EMP_TABLE where Name REGEXP 'Raja|Roja';
The output produced is as shown below −
ID | NAME | DOB | Location |
---|---|---|---|
MyID2 | Raja | 1980-11-06 | Goa |