Modules / Database / REGEXP Lab

Regular Expressions in SQL

When LIKE is not enough. Write a real pattern and watch it match — character by character — against every row, with the equivalent LIKE shown alongside so you can see exactly what regex buys you.

Regular Expressions in SQL: Pattern Matching That Actually Fits

LIKE handles "starts with". Regex handles "a valid email address".

Quick Context

A regular expression describes a shape that text may take, rather than the text itself. SQL's LIKE offers exactly two wildcards; regex offers character classes, quantifiers, anchors, alternation and grouping — enough to validate, extract and clean data directly in the database.

Every Engine Spells It Differently

  • PostgreSQL~ (match), ~* (case-insensitive), !~ (not match). Also SIMILAR TO and functions like REGEXP_REPLACE.
  • MySQL / MariaDBREGEXP or RLIKE.
  • OracleREGEXP_LIKE(), plus REGEXP_SUBSTR and REGEXP_COUNT.
  • SQL Server — historically none; you use LIKE with its limited bracket syntax, or CLR functions. SQL Server 2025 finally adds REGEXP_LIKE.
  • SQLiteREGEXP exists as an operator but the host application must supply the implementation.

The dialect differences are mostly in the operator name; the pattern syntax itself is broadly POSIX or PCRE and transfers well.

LIKE vs REGEXP

Try the email-validation pattern in this lab and read the LIKE equivalent underneath it. LIKE can express "contains an @" and little more; it cannot say "one or more of these characters", "exactly four digits", or "either gmail or yahoo". Those are quantifiers, character classes and alternation — the things LIKE simply does not have.

One more difference that catches people: LIKE 'abc' must match the whole string, whereas most SQL regex operators match if the pattern is found anywhere in it. That is why anchors ^ and $ matter so much — toggle them in this lab and watch the match count change.

The Performance Warning

A regex in a WHERE clause is applied row by row and cannot use a normal B-tree index. On a large table that is a full scan every time. Practical guidance:

  • Narrow the rows with an indexable condition first, then apply the regex to what survives.
  • Prefer LIKE 'abc%' when it suffices — a trailing wildcard can use an index.
  • For repeated searches, consider a functional index, a generated column, or full-text search instead.
  • Validate on write (a CHECK constraint) rather than filtering on read, when you can.

Interactive Exploration Guide

  1. Start with ^A. The anchor forces the match to the start; matched text is highlighted inside each value.
  2. Remove the ^. Suddenly any row containing an "A" anywhere matches. Anchors are the difference between "starts with" and "contains".
  3. Run the email pattern. Malformed addresses in the data fail it — this is validation LIKE cannot perform.
  4. Try the code pattern ^[A-Z]{2}[0-9]{4}$ with case-insensitivity on and off, and watch which rows drop out.
  5. Type a deliberately broken pattern such as [a-. The error is reported rather than silently matching nothing.

Key Takeaway

Reach for regex when the shape of the text matters and LIKE cannot describe it — validation, extraction, messy-data cleanup. Reach for LIKE when a prefix will do, because it can use an index and regex usually cannot. This lab runs your pattern through a real regex engine, so what matches here is what will match in your database.

Cheat sheet

Regular Expressions in SQL

When LIKE is not enough. Write a real pattern and watch it match — character by character — against every row, with the equivalent LIKE shown alongside so you can see exactly what regex buys you.

DATABASE · vizlearn.in/database/regular_expressions_in_sql.html