๐Ÿ”ฌ MySQL REGEXP_INSTR(), REGEXP_LIKE(), REGEXP_REPLACE(), REGEXP_SUBSTR() โ€“ Regex Functions in MySQL 8+


๐Ÿงฒ Introduction โ€“ Why Use REGEXP Functions in MySQL?

With MySQL 8.0, advanced regex functions were introduced for powerful text search, extraction, replacement, and validation using POSIX regular expressions. These include:

  • ๐Ÿ” REGEXP_INSTR() โ€“ Returns position of regex match
  • โœ… REGEXP_LIKE() โ€“ Returns true/false if match exists
  • ๐Ÿ” REGEXP_REPLACE() โ€“ Replaces part of string using regex
  • ๐Ÿ” REGEXP_SUBSTR() โ€“ Extracts substring that matches regex

These functions extend the capability of basic REGEXP and are ideal for data validation, transformation, and search filtering.


๐Ÿงช 1. REGEXP_INSTR() โ€“ Get Match Position

๐Ÿ”น Syntax

REGEXP_INSTR(expr, pattern [, position [, occurrence [, return_option [, match_type]]]])

๐Ÿ”น Example โ€“ Position of Digits

SELECT REGEXP_INSTR('abc123xyz', '[0-9]+') AS match_pos;

โœ… Returns 4 โ†’ First number starts at position 4


๐Ÿ”น Example โ€“ 2nd Occurrence of a Word

SELECT REGEXP_INSTR('cat dog cat bird', 'cat', 1, 2);

โœ… Returns 9 โ†’ Position of second cat


โœ… 2. REGEXP_LIKE() โ€“ Boolean Pattern Check

๐Ÿ”น Syntax

REGEXP_LIKE(expr, pattern [, match_type])

๐Ÿ”น Example โ€“ Check Email Format

SELECT REGEXP_LIKE('test@gmail.com', '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.com$') AS is_valid;

โœ… Returns 1 if it matches the pattern


๐Ÿ”น Use in WHERE Clause

SELECT * FROM users WHERE REGEXP_LIKE(email, '@gmail\\.com$');

โœ… Filters users with Gmail addresses


๐Ÿ” 3. REGEXP_REPLACE() โ€“ Replace with Regex Pattern

๐Ÿ”น Syntax

REGEXP_REPLACE(expr, pattern, replacement [, position [, occurrence [, match_type]]])

๐Ÿ”น Example โ€“ Remove Digits

SELECT REGEXP_REPLACE('user123profile', '[0-9]+', '');

โœ… Output: 'userprofile'


๐Ÿ”น Replace First Occurrence Only

SELECT REGEXP_REPLACE('cat cat cat', 'cat', 'dog', 1, 1);

โœ… Output: 'dog cat cat'


๐Ÿ” 4. REGEXP_SUBSTR() โ€“ Extract Matching Substring

๐Ÿ”น Syntax

REGEXP_SUBSTR(expr, pattern [, position [, occurrence [, match_type]]])

๐Ÿ”น Example โ€“ Extract First Word

SELECT REGEXP_SUBSTR('Hello world 2024', '[A-Za-z]+');

โœ… Output: 'Hello'


๐Ÿ”น Get Numeric Part from String

SELECT REGEXP_SUBSTR('Order #A10123', '[0-9]+');

โœ… Output: '10123'


๐Ÿงฌ 5. Match Types (Optional)

Match Type FlagDescription
'c'Case-sensitive matching
'i'Case-insensitive (default)
'm'Multi-line mode
'n'Dot (.) matches newline

Example:

SELECT REGEXP_LIKE('abc', 'A', 'c'); -- Case-sensitive: returns 0

๐Ÿ“˜ Comparison Table

FunctionPurposeExample Use
REGEXP_INSTR()Get start position of matchFind where a digit starts in a string
REGEXP_LIKE()Return true/false if pattern matchesValidate emails or phone numbers
REGEXP_REPLACE()Replace parts of string using patternStrip digits, mask words, clean inputs
REGEXP_SUBSTR()Extract first (or nth) matched partExtract ID, email domain, or token

๐Ÿš€ Real-World Use Cases

Use CaseRegex Function UsedWhy
Validate phone number formatREGEXP_LIKE()Checks if input matches pattern
Remove special characters from stringREGEXP_REPLACE()Data sanitization
Extract domain from emailREGEXP_SUBSTR()For grouping, statistics, or security
Detect profanity in commentsREGEXP_INSTR()Flag text that contains bad keywords

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

MySQLโ€™s advanced REGEXP_ functions allow for searching, validating, replacing, and extracting text using regular expressionsโ€”far beyond basic LIKE and REGEXP.

๐Ÿ” Key Takeaways

  • REGEXP_INSTR() โ€“ find where a match occurs
  • REGEXP_LIKE() โ€“ check if a match exists (boolean)
  • REGEXP_REPLACE() โ€“ modify matched substrings
  • REGEXP_SUBSTR() โ€“ extract substring that matches pattern
  • Optional match types like 'c', 'i', 'm' allow fine control

โš™๏ธ Real-World Relevance

These functions power form validation, search engines, log filtering, data cleanup, and text analytics in MySQL 8+ environments.


โ“ FAQ โ€“ MySQL Regex Functions


โ“ Are these REGEXP functions available in MySQL 5.x?

โŒ No. These functions require MySQL 8.0+.


โ“ Is REGEXP_LIKE faster than REGEXP in WHERE?

โœ… Yes, REGEXP_LIKE() is optimized for boolean evaluations.


โ“ Can I replace multiple occurrences in REGEXP_REPLACE()?

โœ… Yes. Leave occurrence as default (0) or set it to a high value.


โ“ Is REGEXP case-sensitive?

โœ… By default it’s case-insensitive. Use 'c' flag for case sensitivity.


โ“ Can I use regex on numeric columns?

โŒ No. You must cast numbers to strings using CAST(num AS CHAR).


Share Now :
Share

๐Ÿ”ฌ MySQL regexp_instr(), regexp_like(), regexp_replace(), regexp_substr()

Or Copy Link

CONTENTS
Scroll to Top