regexp_substr snowflake examples

Master REGEXP_SUBSTR in Snowflake: Easy Examples & Syntax

By Steven King
AD

In the realm of data manipulation, regular expressions (regex) are powerful tools that enable you to search, match, and manipulate strings. When it comes to working with data in Snowflake, a cloud-based data warehousing platform, the regexp_substr function is a game-changer. This function allows you to extract substrings based on regular expression patterns. Let's delve into the world of regexp_substr in Snowflake with practical examples.

Understanding regexp_substr in Snowflake

The regexp_substr function in Snowflake takes four main arguments: the input string, the regular expression pattern, the start position, and the occurrence number. The syntax is as follows:

regexp_substr(input_string, pattern, start_position, occurrence_number)

Input String

The input string is the text from which you want to extract substrings.

AD
Snowflake Method: 8 Easy Steps + Free Templates | Imagine Forest
Snowflake Method: 8 Easy Steps + Free Templates | Imagine Forest
the types of snowflakes
the types of snowflakes
the types of snowflakes are shown in black and white, with text below
the types of snowflakes are shown in black and white, with text below

Pattern

The pattern is a regular expression that defines the substring you want to extract. Regular expressions use special syntax to match patterns in text.

Start Position

The start position determines where in the input string the search for the pattern begins. It's 1-based, meaning the first character is position 1.

Difference Between Star and Snowflake Schema
Difference Between Star and Snowflake Schema
a black and white version of the game click to view with all its basic instructions
a black and white version of the game click to view with all its basic instructions
🔐 Snowflake RBAC Explained!
🔐 Snowflake RBAC Explained!

Occurrence Number

The occurrence number specifies which match to return. For example, if there are multiple matches, setting the occurrence number to 1 will return the first match, 2 will return the second match, and so on.

Basic regexp_substr Examples

Let's start with some basic examples to illustrate the usage of regexp_substr.

AD

Extracting Email Addresses

Suppose you have a text column containing customer feedback, and you want to extract email addresses. You can use the following pattern to match email addresses:

FREEBIES FOR WINTER - types of snowflakes - kindergarten lesson science
FREEBIES FOR WINTER - types of snowflakes - kindergarten lesson science
snowflakes are shown in blue and white, with the same amount as each other
snowflakes are shown in blue and white, with the same amount as each other
Snowflakes Theme Printables
Snowflakes Theme Printables
the types of snowflakes
the types of snowflakes
Grab your copy of these free printable snowflake templates and make your own. Paper snowflakes are a classic craft that kids of all ages will love. Make them with your kids this Winter or Christmas, Winter Design Ideas, Snow Flakes Paper, Snowflake Outlines, Snowflake Templates Easy, Paper Snow Flakes Templates, Simple Paper Snowflakes, Easy Snowflake Template Printable Free, Easy Snowflake Cutout, Paper Snowflake Patterns Easy
Grab your copy of these free printable snowflake templates and make your own. Paper snowflakes are a classic craft that kids of all ages will love. Make them with your kids this Winter or Christmas, Winter Design Ideas, Snow Flakes Paper, Snowflake Outlines, Snowflake Templates Easy, Paper Snow Flakes Templates, Simple Paper Snowflakes, Easy Snowflake Template Printable Free, Easy Snowflake Cutout, Paper Snowflake Patterns Easy
Free Snowflake Life Cycle Worksheet (Winter Science Printable)
Free Snowflake Life Cycle Worksheet (Winter Science Printable)
SELECT regexp_substr('Send feedback to john@example.com or jane.doe@gmail.com', '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', 1, 1)

This will return the first email address found in the input string.

Extracting Phone Numbers

To extract phone numbers in the format (123) 456-7890, you can use the following pattern:

AD
SELECT regexp_substr('Call us at (123) 456-7890 or (987) 654-3210', '\(\d{3}\) \d{3}-\d{4}', 1, 1)

This will return the first phone number found in the input string.

the science of snowflakes is shown in this graphic, which shows how they are made
the science of snowflakes is shown in this graphic, which shows how they are made
several snowflakes are shown in different colors
several snowflakes are shown in different colors
a poster with snowflakes and other things on it's black back ground
a poster with snowflakes and other things on it's black back ground
Snowflake Dynamic Tables
Snowflake Dynamic Tables
snowflakes are shown in black and white, with different shapes to choose from
snowflakes are shown in black and white, with different shapes to choose from
Functions in Snowflake (2025): Complete Guide for Data Transformation & Analytics
Functions in Snowflake (2025): Complete Guide for Data Transformation & Analytics
Frozen Snowflake Symmetry Resist Art Winter Activity
Frozen Snowflake Symmetry Resist Art Winter Activity
several snowflakes are shown in four different pictures
several snowflakes are shown in four different pictures
snowflakes are shown in black and white on a black background with silver foil
snowflakes are shown in black and white on a black background with silver foil
How to Make Paper Snowflakes
How to Make Paper Snowflakes
six different snowflakes are shown in multiple pictures
six different snowflakes are shown in multiple pictures
an image of different types of snowflakes in chinese writing and english characters on paper
an image of different types of snowflakes in chinese writing and english characters on paper
snowflakes are shown on a blue background with white letters and numbers below them
snowflakes are shown on a blue background with white letters and numbers below them
Types of Snowflakes Print Homeschool
Types of Snowflakes Print Homeschool

Using regexp_substr with Wildcard Characters

Regular expressions support wildcard characters that can match any single character. The most common wildcard characters are:

  • . - Matches any single character except newline.
  • ^ - Matches the start of the string.
  • $ - Matches the end of the string.

Extracting Words Starting with 'S'

To extract all words starting with 'S' from a text column, you can use the following pattern:

AD
SELECT regexp_substr('The sun sets in the south.', '^S\w*', 1, 1)

This will return 'Sun' as the first word starting with 'S'.

Extracting Multiple Occurrences with regexp_substr

By default, regexp_substr returns only the first match. However, you can extract multiple occurrences by using the ALL keyword as the occurrence number.

Extracting All Email Addresses

To extract all email addresses from a text column, you can use the following query:

SELECT regexp_substr_all('Send feedback to john@example.com or jane.doe@gmail.com', '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}')

This will return a table with two rows, each containing one email address.

Performance Considerations

While regexp_substr is a powerful function, it's important to note that regular expressions can be resource-intensive, especially when dealing with large datasets. To optimize performance, consider the following:

  • Use simple patterns whenever possible.
  • Limit the search space by using the start position argument.
  • Consider using the RLIKE operator for simple pattern matching instead of regexp_substr.

Conclusion

The regexp_substr function in Snowflake is a versatile tool for extracting substrings based on regular expression patterns. Whether you're extracting email addresses, phone numbers, or specific words, regexp_substr can help you manipulate and analyze your data more effectively. By understanding its syntax and using it judiciously, you can unlock the full potential of regular expressions in Snowflake.

AD