Regular Expressions in SQL

Last Updated : 14 Mar, 2026

Regular expressions (regex) are powerful tools used to search, match, extract, and replace text based on specific patterns.In SQL, regex helps manage and manipulate textual data more efficiently than simple string functions and makes it useful for handling complex data-processing tasks.

  • Regex stands for Regular Expression.
  • It is a pattern used to match text.
  • Regex is used to find, validate, extract, and replace data.
  • It is widely used in SQL for text processing.

Example:

SELECT email 
FROM users
WHERE REGEXP_LIKE(email, '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$');

This query retrieves all emails from the users table that match the regex pattern for valid email addresses.

Types of Regular Expressions in SQL

In SQL, there are three primary functions for working with regular expressions:

1. REGEXP_LIKE

REGEXP_LIKE checks whether a string matches a given regular expression and returns TRUE or FALSE. It is commonly used in WHERE clauses to filter rows that match a specific text pattern.

Syntax:

REGEXP_LIKE(column_name, 'pattern')

Query:

The following query selects all product names from the products table where the names start with the letter 'A':

SELECT product_name 
FROM products
WHERE REGEXP_LIKE(product_name, '^A');

Output:

Screenshot-2026-01-31-160901

2. REGEXP_REPLACE

The REGEXP_REPLACE function in SQL is used to search for a pattern within a string and replace all occurrences of that pattern with a specified replacement string. This function is particularly useful for cleaning and transforming data by removing unwanted characters or formatting strings.

Syntax:

REGEXP_REPLACE(string, 'pattern', 'replacement')

Query:

The following query removes all non-numeric characters from the phone_number column in the contacts table:

SELECT REGEXP_REPLACE(phone_number, '[^0-9]', '') AS cleaned_number 
FROM contacts;

Output:

Screenshot-2026-01-31-161131

3. REGEXP_SUBSTR

REGEXP_SUBSTR extracts a part of a string that matches a given regular expression, making it useful for pulling specific patterns like emails or numbers.

Syntax:

REGEXP_SUBSTR(string, 'pattern', start_position, occurrence, match_parameter)

Query:

To extract the domain name from the email field in the users table:

SELECT REGEXP_SUBSTR(email, '@[^.]+') AS domain 
FROM users;

Output:

Screenshot-2026-01-31-161747

Basic Regular Expression Syntax Table

Regular expressions (regex) are constructed using a combination of characters and special symbols, each with specific meanings. This table format organizes regex elements and examples for quick reference, making it easy to understand and apply them in practical scenarios.

PatternDescriptionExampleMatches
.Matches any single character (except newline)h.that, hit, hot
^Matches the start of a string^AApple, Apricot
$Matches the end of a stringing$sing, bring
|Acts as logical ORcat|dogcat, dog
*Zero or more of previous characterab*a, ab, abb
+One or more of previous characterab+ab, abb
?Zero or one of previous charactercolou?rcolor, colour
{n}Exactly n timesa{3}aaa
{n,}n or more timesa{2,}aa, aaa
{n,m}Between n and m timesa{2,4}aa, aaa, aaaa
[abc]Any one character inside[aeiou]a, e, i, o, u
[^abc]Any character not inside[^aeiou]any non-vowel
[a-z]Character range[0-9]0–9
\Escapes special character\..
\bWord boundary\bcat\bcat (not scatter)
\BNon-word boundary\Bcatscatter
(abc)Grouping(ha)+ha, haha
\1Back-reference(ab)\1abab

Common REGEX Patterns

Common Regex Patterns are used to match and find specific text patterns.

PatternDescriptionExampleMatches
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$Validates an email address.john.doe@gmail.comValid email addresses
^[0-9]+$Matches a numeric string only.123456123, 456, 7890
https?://[^ ]+Matches a URL starting with http or https.https://example.com/URLs
^[A-Za-z0-9]+$Matches alphanumeric strings.User123abc123, xyz789

Real-World Examples of Regular Expressions in SQL

Here’s how regular expressions can solve common data processing tasks in SQL:

Example 1: Extracting URLs from Text

If we have a messages table containing text with embedded URLs, we can extract the URLs as follows. This regex matches URLs starting with http:// or https:// and extracts them.

Query:

SELECT REGEXP_SUBSTR(message, 'https?://[^ ]+') AS url 
FROM messages;
  • https?://: Matches http:// or https://.
  • [^ ]+: Matches all characters up to the next space.

Example 2: Validating Email Addresses

To validate email addresses in the users table. This pattern ensures that the email follows the standard format.

SELECT email 
FROM users
WHERE REGEXP_LIKE(email, '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$');

Ensures the email address starts with alphanumeric characters, includes an @ symbol, followed by a domain, and ends with a valid TLD (top-level domain).

Example 3: Cleaning Up Phone Numbers

To remove non-numeric characters from phone numbers in the contacts table. This query leaves only numeric characters in the phone_number field.

SELECT REGEXP_REPLACE(phone_number, '[^0-9]', '') AS cleaned_number 
FROM contacts;
  • [^0-9]: Matches any character that is not a digit.
  • ' ': Replaces non-numeric characters with an empty string.

Example 4: Finding Specific Patterns

Find all product names in the products table that contain digits:

SELECT product_name 
FROM products
WHERE REGEXP_LIKE(product_name, '[0-9]');

Example 5: Extracting Subdomains

Extract the subdomain from URLs in the web_logs table:

SELECT REGEXP_SUBSTR(url, '^[^.]+') AS subdomain 
FROM web_logs;
  • ^[^.]+: Matches all characters from the start of the string up to the first . (dot).

Example 6: Validating Numeric Strings

Find records where a field contains only numbers in the data_table:

SELECT record_id 
FROM data_table
WHERE REGEXP_LIKE(field_name, '^[0-9]+$');
  • ^[0-9]+$: Matches strings that consist entirely of digits.

Regular Expression Use Cases in SQL

Here are some common regular expression use cases in SQL:

  • Data Validation: Checks if data follows a required format (email, phone number, numbers).
  • Data Cleaning: Removes unwanted characters or extra spaces.
  • Data Extraction: Pulls useful parts like domains from emails or URLs from text.
Comment