MySQL String Functions: Regex Explained

Formulas & functions · Updated November 23, 2026

MySQL does not have a single “regex” function, but it provides a set of string functions that use regular expressions for pattern matching and manipulation: REGEXP (and its synonym RLIKE) for matching, REGEXP_LIKE(), REGEXP_REPLACE(), and REGEXP_SUBSTR() (all available since MySQL 8.0). For older versions (5.x), only REGEXP matching is available, without replacement or extraction. This article explains each function, gives concrete examples, and covers common pitfalls.

Background: How MySQL Handles Regex

MySQL’s regular expression support uses the International Components for Unicode (ICU) library since version 8.0. This means the regex syntax is POSIX-extended with some enhancements, but it differs from Perl-compatible (PCRE) syntax in important ways. For instance, MySQL uses \d for digits (ICU supports this), but it does not support lookaheads or backreferences. In MySQL 5.7 and earlier, the regex engine was Henry Spencer’s implementation, which had even fewer features.

Key points about MySQL regex:

  • REGEXP and RLIKE return 1 if the pattern matches, 0 if not, and NULL if either argument is NULL.
  • Matching is case-insensitive by default for non-binary strings, unless you use the BINARY keyword or a case-sensitive collation.
  • Patterns are not anchored by default; they match anywhere in the string unless you use ^ (start) or $ (end).
  • To use a literal backslash in a pattern, you must escape it with another backslash in the string literal (e.g., '\\.' to match a dot).

Core Functions: REGEXP, REGEXP_LIKE, REGEXP_REPLACE, REGEXP_SUBSTR

Here are the four main functions you will use:

  • expr REGEXP pattern – returns 1 if expr matches pattern. Example: SELECT 'abc' REGEXP '^a'; returns 1.
  • REGEXP_LIKE(expr, pattern) – same as REGEXP but as a function. Example: SELECT REGEXP_LIKE('abc', 'b'); returns 1.
  • REGEXP_REPLACE(expr, pattern, replacement) – replaces all occurrences of the pattern with the replacement string. Example: SELECT REGEXP_REPLACE('a1b2', '[0-9]', ''); returns ‘ab’.
  • REGEXP_SUBSTR(expr, pattern) – returns the first substring that matches the pattern. Example: SELECT REGEXP_SUBSTR('abc123', '[0-9]+'); returns ‘123’.

All these functions are available in MySQL 8.0 and later. If you are on MySQL 5.7, you only have REGEXP and RLIKE; you would need to use LIKE with wildcards or write a stored function for replacement/extraction.

How to Use Regex in WHERE Clauses

You often use regex to filter rows. For example, to find all customers whose email is from a .org domain:

SELECT email FROM customers WHERE email REGEXP '@[a-zA-Z0-9.-]+\.org$';

This pattern matches an @, then one or more alphanumeric, dot, or hyphen characters, then a literal dot (escaped with \.), then ‘org’ at the end. Note the double backslash in the string literal – MySQL interprets \\ as a single backslash in the string, which is then passed to the regex engine.

For case-insensitive matching, MySQL’s default collation is usually case-insensitive, so 'ABC' REGEXP 'abc' returns 1. To force case sensitivity, use BINARY: SELECT BINARY 'ABC' REGEXP 'abc'; returns 0.

Common Regex Patterns and Examples

Here are practical examples you might use:

  • Find numeric-only strings: SELECT '123' REGEXP '^[0-9]+$'; returns 1.
  • Extract phone numbers: SELECT REGEXP_SUBSTR('Call 555-1234 now', '[0-9]{3}-[0-9]{4}'); returns ‘555-1234’.
  • Clean up data: SELECT REGEXP_REPLACE(' Hello World ', ' +', ' '); collapses multiple spaces to one, returning ‘ Hello World ‘.
  • Validate email format (simple): SELECT 'user@example.com' REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'; returns 1.

Remember that regex in MySQL is not as powerful as PCRE. For complex validation, you might need to combine multiple regex tests or use a stored procedure.

Performance and Indexing Considerations

Using REGEXP in a WHERE clause prevents the use of regular indexes, because the server must scan every row and evaluate the pattern. For large tables, this can be slow. If you frequently search for a specific pattern, consider:

  • Storing a normalized column (e.g., a cleaned version) and indexing that.
  • Using LIKE for simple prefix/suffix matches, which can use indexes if the pattern starts with a literal.
  • Using a full-text index for word-based searches, not regex.

For example, WHERE email LIKE 'user@%' can use an index on email, while WHERE email REGEXP '^user@' cannot.

Alternatives and Comparisons

If you need more regex power, you have alternatives:

  • LIKE operator – uses % and _ wildcards, faster but less flexible. Good for simple patterns.
  • Full-text search – for natural language searching, not regex.
  • MariaDB – its regex functions are similar, but MariaDB also supports PCRE syntax if you enable the pcre library (in newer versions).
  • Application-side regex – fetch rows with simple LIKE or a broad condition, then apply full regex in PHP, Python, or Java. This offloads CPU but transfers more data.

For example, if you need to extract a complex pattern like an IP address with validation, doing it in the application might be easier because PCRE supports lookaheads and backreferences.

Common Pitfalls and Troubleshooting

Here are frequent mistakes and how to fix them:

  • Forgetting to escape backslashes: In MySQL strings, a backslash is an escape character. To match a literal backslash, you need four: '\\\\' in a string literal. To match a dot, use '\.'.
  • Expecting PCRE features: Lookaheads like (?=...) and backreferences like \1 are not supported in MySQL 8.0’s ICU engine. Use alternatives or app-side processing.
  • Case sensitivity surprises: If your column has a case-sensitive collation (e.g., utf8mb4_bin), regex will be case-sensitive. Use COLLATE to change it if needed.
  • NULL handling: If any argument is NULL, the result is NULL, not 0. Use IFNULL or COALESCE if you need a default.
  • Regex injection: If you build patterns from user input, escape regex metacharacters to avoid unexpected matches or denial-of-service via catastrophic backtracking (less of a risk in ICU, but still).

Frequently Asked Questions

Q: Can I use regex in MySQL 5.7?
Yes, you can use REGEXP and RLIKE for matching, but not REGEXP_REPLACE or REGEXP_SUBSTR. Those were added in 8.0. For 5.7, you can simulate replacement with a stored function or use REPLACE() for simple patterns.

Q: How do I make regex case-sensitive?
Use the BINARY keyword before the string, or ensure the column collation is case-sensitive. For example: SELECT * FROM t WHERE BINARY name REGEXP '^john'; will not match ‘John’.

Q: What is the difference between REGEXP and LIKE?
LIKE uses % and _ wildcards and is faster because it can use indexes for prefix matches. REGEXP uses full regular expressions, which are more flexible but slower and cannot use indexes.

Q: How do I extract all matches, not just the first?
MySQL does not have a built-in function to return all matches as a set. You can use REGEXP_REPLACE to remove matches and loop in a stored procedure, or handle it in your application code.

Q: Does MySQL support lookahead or backreferences?
No. MySQL 8.0 uses ICU, which does not support lookaheads or backreferences. For those, you need to use a different tool or process the data outside MySQL.

Q: Why does my pattern with a dot not match a period?
In regex, a dot matches any character. To match a literal period, escape it with a backslash: '\.' in the pattern. In MySQL string literals, you write that as '\.' (two characters: backslash and dot).

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.