MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL REGEXP Operator

MySQL has another type of pattern matching known as extended regular expressions. It performs a pattern match of a string expression against a pattern. If the pattern finds a match in the expression, it returns 1, else it returns 0.

When you test for a match pattern, use the REGEXP and NOT REGEXP operators.

MetacharacterDescriptions
^Matches the position at the beginning of the searched string
$Matches the position at the end of the searched string
.Matches any single character
[…]Matches any character specified inside the square brackets
[^…]Matches any character not specified inside the square brackets
p1|p2Matches any of the patterns p1 or p2
*Matches the preceding character zero or more times
+Matches preceding character one or more times
{n}Matches n number of instances of the preceding character
{m,n}Matches from m to n number of instances of the preceding character

MySQL REGEXP OPERATOR Syntax

To performs a match pattern of a string expression against a pattern, use the following syntax:

Syntax

SELECT col1, col2, col3... FROM tablename
WHERE col1 REGEXP 'expression'; 

Parameters:

tablename: The table name from which you want to fetch the records.

REGEXP: To performs a match pattern of a string expression against a pattern.

MySQL REGEXP OPERATOR example 1

The following MySQL statement is used to performs a match pattern of a string expression against a pattern:

Example

SELECT firstName, lastName, address FROM tblCustomerLoan
WHERE firstName REGEXP '^(M|R|D)';

Note:

In the above example, we are showing how to find out "firstName" whose last name starts with character A, B or C.

You can use MySQL Command Line Client to performs a match pattern of a string expression against a pattern. It will look like this:

MySQL REGEXP OPERATOR example 2

The following MySQL statement is used to performs a match pattern of a string expression against a pattern:

Example

SELECT firstName, lastName, address FROM tblCustomerLoan
WHERE address REGEXP 'PVR';

Note:

In the above example, we are showing how to find the "address" whose name contains "PVR", you use the above query:

You can use MySQL Command Line Client to performs a match pattern of a string expression against a pattern. It will look like this: