MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL REGEXP: Regular Expression

REGEXP is a powerful feature in MySQL that allows for complex pattern matching using regular expressions.

Consider the following employees table:

idname
1John
2Jane
3Alice
4Bob
5Charlie
6David

Here are some examples of how you can use REGEXP:

Basic Usage

If you want to find all names that start with 'J', you can use REGEXP like this:

SELECT * FROM employees WHERE name REGEXP '^J';

This will return:

idname
1John
2Jane

Using a Character Set

You can also use a character set to match any single character within the set. For example, to find names that start with either 'A', 'B', or 'C', you can do:

SELECT * FROM employees WHERE name REGEXP '^[ABC]';

This will return:

idname
3Alice
4Bob
5Charlie

Using a Dot

The dot . is a special character that matches any single character. For instance, to find names that have any character followed by 'o', you can do:

SELECT * FROM employees WHERE name REGEXP '.o';

This will return:

idname
1John
5Charlie

Negation

You can also use NOT REGEXP to find rows that do not match the pattern. For example, to find names that do not start with 'J', you can do:

SELECT * FROM employees WHERE name NOT REGEXP '^J';

This will return:

idname
3Alice
4Bob
5Charlie
6David

Remember, regular expressions can be complex, but they're also very powerful. They're a great tool to have in your toolbox for situations where standard SQL LIKE operator is not enough.

  1. MySQL REGEXP Operator Example:

    • Description: The REGEXP operator is used for pattern matching using regular expressions.
    • Example Code:
      SELECT * FROM table_name
      WHERE column_name REGEXP 'pattern';
      
  2. How to Use REGEXP in MySQL:

    • Description: Use REGEXP to perform pattern matching in a WHERE clause.
    • Example Code:
      SELECT * FROM emails
      WHERE email_address REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$';
      
  3. Regular Expression Matching in MySQL:

    • Description: REGEXP allows you to match complex patterns in string data.
    • Example Code:
      SELECT * FROM sentences
      WHERE sentence_text REGEXP '\\bword\\b';
      
  4. Case-sensitive vs Case-insensitive REGEXP in MySQL:

    • Description: By default, REGEXP is case-sensitive. Use the BINARY keyword for case-sensitive matching.
    • Example Code:
      SELECT * FROM names
      WHERE last_name REGEXP BINARY 'Smith';
      
  5. Negating Regular Expressions with NOT REGEXP in MySQL:

    • Description: Use NOT REGEXP to filter out rows that match the specified regular expression.
    • Example Code:
      SELECT * FROM products
      WHERE product_name NOT REGEXP 'special_offer';
      
  6. Using Character Classes in MySQL REGEXP:

    • Description: Character classes [ ] allow you to match any one of a set of characters.
    • Example Code:
      SELECT * FROM words
      WHERE word REGEXP '[aeiou]';
      
  7. Optimizing Regular Expression Queries in MySQL:

    • Description: Regular expressions can be resource-intensive. Optimize queries by indexing columns or using more specific patterns.
    • Example Code:
      CREATE INDEX idx_pattern_optimize ON table_name (column_name);
      
  8. Examples of Using REGEXP in MySQL Queries:

    • Description: Various examples showcasing the application of REGEXP in different scenarios.
    • Example Code:
      SELECT * FROM phone_numbers
      WHERE phone_number REGEXP '^\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}$';