Knowledge base / SQL
SQL regular expressions: patterns, anchors and engine syntax
I use short codes to distinguish substring matching from a whole-value check and explain why regex syntax needs an explicit database dialect.

A regular expression describes a text pattern, such as a prefix, digit count or permitted characters. I begin by identifying the database engine and version. REGEXP is not a universal keyword that can be pasted into every SQL dialect. It is equally important to decide whether the task searches for a fragment or checks the shape of an entire value.
Which form does your database support?
This table shows specific forms rather than promising compatibility between regex dialects. Engines also differ in flags, character classes and support for more advanced constructs.
| Environment | Typical condition |
|---|---|
| MySQL 8.4 | REGEXP_LIKE(code, pattern, 'c') or the REGEXP operator |
| PostgreSQL | code ~ pattern, or code ~* pattern to ignore case |
| SQL Server 2025 | REGEXP_LIKE(code, pattern, 'c'), compatibility level at least 170 |
| SQLite | REGEXP requires an application supplied regexp() function |
MySQL documents REGEXP as a synonym for REGEXP_LIKE. PostgreSQL describes its own operators, while Microsoft specifies version and compatibility requirements. SQLite does not define regexp by default. An operator appearing in syntax documentation does not establish that every application already provides its implementation.
Begin with a small format check
The examples below use MySQL 8.4, short single-line strings and synthetic data. I want an exact AB- prefix followed by three ASCII digits. The c flag explicitly requests case-sensitive matching.
SELECT
REGEXP_LIKE('AB-123', '^AB-[0-9]{3}$', 'c') AS correct_code,
REGEXP_LIKE('ab-123', '^AB-[0-9]{3}$', 'c') AS lower_case,
REGEXP_LIKE('XAB-123', '^AB-[0-9]{3}$', 'c') AS extra_prefix;
The expected values are 1, 0 and 0. ^ marks the beginning, AB- is literal text, [0-9] permits a digit in that range, {3} requires three occurrences and $ closes the pattern at the end. AB-1234 also fails. Multiline flags and line terminators require separate consideration, so they are outside this introductory example.
This checks the shape of a value. It does not prove that a code exists in a product catalogue or belongs to an active product. Those questions require comparison with the relevant source. Likewise, a pattern resembling an email address or telephone number cannot establish that the contact works. This entry does not attempt complete validation of those formats.
Distinguish a fragment from the whole value
Without anchors, a regex can match part of the text. In MySQL 8.4, REGEXP_LIKE('XAB-123Z', 'AB-[0-9]{3}', 'c') matches because the required sequence occurs inside the value. The same text fails the anchored ^AB-[0-9]{3}$ rule. That distinction matters more than how long or impressive the query looks.
I also avoid carrying wildcard meanings directly from LIKE. In regex, % does not mean any sequence, and _ is not a universal single-character wildcard. I choose the matching mechanism for the question and check its own syntax. A simple prefix condition can often be expressed clearly with LIKE.
Dots, repetition and two parsing layers
A dot is a regex metacharacter. To require a literal dot between v1 and 2, I can use [.]. This example avoids backslashes and changes only that one detail:
SELECT
REGEXP_LIKE('v1x2', '^v1.2$', 'c') AS any_character,
REGEXP_LIKE('v1x2', '^v1[.]2$', 'c') AS literal_dot;
The expected results are 1 and 0. The first pattern permits x where the dot appears, while the second requires an actual dot. Similarly, [0-9]{3} requires three consecutive digits, whereas [0-9]+ requires one or more. Those are different conditions. When the exact number matters, I state it rather than treating all repetition symbols as interchangeable.
MySQL interprets the SQL string literal before the regex engine receives its pattern. Under default backslash processing, a literal backslash needs appropriate doubling, as the MySQL documentation explains. I therefore avoid copying a lone \d from instructions for another language. These examples use [0-9], while NO_BACKSLASH_ESCAPES remains a separate environment setting to consider.

Express the same intention in PostgreSQL
With an ordinary supported PostgreSQL collation, I can use ~. For the same single-line codes, the query is:
SELECT code
FROM (VALUES ('AB-123'), ('ab-123'), ('XAB-123')) AS t(code)
WHERE code ~ '^AB-[0-9]{3}$'
ORDER BY code;
Only AB-123 matches. The ~* operator would ignore case. This query is for PostgreSQL, not MySQL syntax. Transferring an intention requires checking the mechanism, and more complicated patterns can require further adaptation. Identical function names in two databases do not establish that their regex engines are identical.
NULL, invalid patterns and execution cost
In MySQL, REGEXP_LIKE returns NULL if an argument is NULL. Negating the condition does not turn missing data into a match. If missing values need their own list, I select them with IS NULL. An invalid pattern is a different problem: it is an error in the rule and may make the query fail. Missing input, nonmatching text and invalid expressions need separate treatment.
I would not promise faster matching after adding an arbitrary index. PostgreSQL provides specific capabilities through pg_trgm, but usefulness depends on the pattern and query. An ordinary column index is not a general solution for regex. I first narrow the data according to the question, then assess execution cost using the relevant engine’s plan.
For practice, compare AB-123, AB-12, AB-1234, ab-123 and XAB-123 against the anchored rule above. Remove the anchors and describe the changed meaning. Explaining why each value matches or fails gives you a better basis for extending a pattern than adding symbols through trial and error.
Key takeaways
Does REGEXP work identically in every database?
No. MySQL offers REGEXP and REGEXP_LIKE, PostgreSQL has its own operators, and SQL Server adds REGEXP_LIKE in 2025 with the required compatibility level.
How do I match a complete code?
For the simple single-line example, use beginning and end anchors, such as ^AB-[0-9]{3}$. Flags and line endings require checking the engine rules.
Does a matching pattern prove that a product exists?
No. It establishes only that a text rule was met. Product existence and current status require the relevant catalogue.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
