Regular Expressions in Tableau
top of page
  • Bernard K

Regular Expressions in Tableau


regular expressions in tableau

Introduction

Regular Expressions (shortened as REGEX or REGEXP) is a sequence of characters that specifies a match pattern in a text. Usually, such patterns are used by string-searching algorithms for ‘find’ or ‘find and replace’ operations on strings or for input validation.

A basic example of regular expression would be regex /c*t/ which would match “cat”, “cot”, or “cut”, but not “pat” or “but”.

Tableau offers four REGEX functions which include.

  • REGEXP_EXTRACT (string, pattern) – which extracts a text from a string field that matches the specified REGEX pattern.

  • REGEXP_EXTRACT_NTH (string, pattern, index) – which is executed same as above, only that it extracts the nth matching group specified by the index.

  • REGEXP_MATCH (string, pattern) – which returns TRUE if pattern is matched in the string field.

  • REGEXP_REPLACE (string, pattern, replacement) – Which replaces the matched pattern with the specified replacement text.

In this short article, I will be sharing three use cases of regular expressions in Tableau.

Case 1: Extracting email addresses from a string of text.

The regular expression pattern for email addresses is,

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}

To demonstrate this, I have connected the following text data which contains customer email addresses - the goal here is to demonstrate how you can use REGEX functionality to extract the email addresses.

To extract the email addresses from this text data, I will create the following calculation.

using regular expressions to extract email addresses in Tableau

Note: The pattern in this calculation is enclosed in parentheses (capture group) - so Tableau can treat the multiple characters as a single unit.

Adding the calculation to the view.

(Which extracts each customer’s email address).

Case 2: Extracting a URL

The regular expression pattern for extracting URLs is,

(http|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?

Just like in the first case, you can extract URLs from text data using the following REGEX calculation.

using regular expressions to extracts URLs in Tableau

Note: The pattern in this calculation is enclosed in parentheses (capture group) - so Tableau can treat the multiple characters as a single unit.

Executing this calculation extracts the URLs from the text data.

Case 3: Extracting numbers from a string of text

Using Regular expressions, you can extract numbers from a string of text using the following calculation.

Pattern: [0-9]+

using regular expressions to extract numbers from a string of text data.

Note: The pattern in this calculation is enclosed in parentheses (capture group) - so Tableau can treat the multiple characters as a single unit.

Executing this calculation extracts the numbers from the text.

Conclusion

I hope this article was helpful to you. If you wish to receive more Tableau tips and tricks, kindly join our mailing list by subscribing below.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Blog.png
Black & white.jpg

About Me

More About the Author

Bernard K

Analytics Consultant | 3X Tableau Certified

Bernard is a data analytics consultant helping businesses reveal the true power of their data and bring clarity to their reporting dashboards. He loves building things and sharing knowledge on how to build dashboards that drive better outcomes.

Let’s discuss your data challenges! Let’s work together!

bottom of page