MySQL REGEXP, RLIKE, and NOT REGEXP โ€“ Advanced Pattern Matching Explained


Introduction โ€“ What Is REGEXP in MySQL?

MySQL’s REGEXP operator allows for regular expression-based pattern matching, providing far more flexibility than LIKE. It’s used for:

  • Validating text formats
  • Advanced searching (e.g., ranges, character classes)
  • Filtering text that does not match a pattern using NOT REGEXP

In this guide, youโ€™ll learn:

  • How to use REGEXP, RLIKE, and NOT REGEXP
  • Syntax and supported regular expression features
  • Real-world examples
  • Tips and performance notes

1. REGEXP โ€“ Match Using Regular Expressions

Syntax

SELECT * FROM table_name WHERE column REGEXP 'pattern';

Returns rows where column matches the regex pattern.


Example โ€“ Names Starting with A or B

SELECT name FROM employees WHERE name REGEXP '^[AB]';

Matches: Alice, Ben
Skips: Charlie, David


2. RLIKE โ€“ Synonym for REGEXP

SELECT name FROM employees WHERE name RLIKE 'son$';

Equivalent to REGEXP 'son$'
Use eitherโ€”they behave exactly the same.


3. NOT REGEXP โ€“ Invert Match Logic

SELECT name FROM customers WHERE email NOT REGEXP '@gmail\\.com$';

Returns customers whose emails do not end with @gmail.com.


4. Common REGEXP Patterns in MySQL

PatternDescriptionExample Match
^abcStarts with abcabcdef, abc123
xyz$Ends with xyzabcxyz, 123xyz
a.ba followed by any one character, then bacb, a_b
[aeiou]Any vowelapple, echo
[^0-9]Not a digitabc, Z
[A-Z]{2,4}2โ€“4 uppercase lettersAB, XYZ, TEST
`foobar`Match foo or bar
[0-9]{5}Exactly 5 digits12345, 67890

Escaping Special Characters

Use \\ to escape regex meta characters:

SELECT email FROM users WHERE email REGEXP '\\.edu$';

Matches emails ending with .edu


5. Case Sensitivity in REGEXP

  • REGEXP is case-insensitive by default in MySQL:
SELECT 'apple' REGEXP 'A'; -- Returns 1 (match)
  • Use BINARY for case-sensitive regex:
SELECT name FROM clients WHERE BINARY name REGEXP '^A';

Matches only names starting with uppercase A


6. REGEXP Use Cases

Use CasePattern/Usage
Validate US ZIP codeREGEXP '^[0-9]{5}$'
Match phone numbers with hyphensREGEXP '^[0-9]{3}-[0-9]{3}-[0-9]{4}$'
Filter invalid usernamesNOT REGEXP '^[a-zA-Z0-9_]+$'
Check for special charactersREGEXP '[^a-zA-Z0-9]'
Find names ending in โ€œsonโ€REGEXP 'son$'

REGEXP vs LIKE โ€“ Key Differences

FeatureLIKEREGEXP / RLIKE
SyntaxSimple wildcardsFull regular expressions
Operators%, _^, $, [a-z], *, +
PerformanceFaster, indexableSlower, non-indexed
FlexibilityLimitedHigh
Case sensitivityBased on collationCase-insensitive by default

Performance Tips

  • Avoid using REGEXP on large datasets unless absolutely needed
  • Prefer indexed LIKE 'prefix%' queries for performance
  • Test regex patterns thoroughly before production usage
  • Use REGEXP with filtered subsets (WHERE type = 'email' AND ...)

Summary โ€“ Recap & Next Steps

MySQLโ€™s REGEXP, RLIKE, and NOT REGEXP provide powerful pattern-matching capabilities that go far beyond LIKE. Use them when you need flexible validation, complex filtering, or character-level matching.

Key Takeaways

  • Use REGEXP or RLIKE for regular expression matching
  • Use NOT REGEXP to find rows that do not match a pattern
  • Escape characters carefully (e.g., \\. for .)
  • REGEXP is case-insensitive unless BINARY is used
  • Ideal for validating formats, filtering by patterns, and advanced searches

FAQ โ€“ REGEXP / RLIKE / NOT REGEXP


Are REGEXP and RLIKE the same?

Yes. RLIKE is an alias for REGEXP.


Can I use REGEXP in WHERE clause?

Yes. It’s designed for pattern matching in WHERE.


Is REGEXP case-sensitive?

No, by default. Use BINARY for case-sensitive matching.


Can I use multiple regex conditions?

Yes, using alternation or AND/OR:

REGEXP 'foo|bar' -- matches either

Is REGEXP slower than LIKE?

Yes. REGEXP cannot use indexes, so itโ€™s slower on large tables.


Share Now :
Share

๐Ÿ”ค MySQL REGEXP / RLIKE / NOT REGEXP

Or Copy Link

CONTENTS
Scroll to Top