Using REGEX() functions in Airtable
  • 31 Jan 2024
  • 3 Minutes to read
  • Dark
    Light
  • PDF

Using REGEX() functions in Airtable

  • Dark
    Light
  • PDF

Article Summary

This article covers how to use REGEX() to streamline your team's and organization's work.

Plan availability

All plan types

Platform(s)

Web/Browser, Mac app, and Windows app

Related reading

REGEX() in Airtable overview

REGEX() overview

NOTE

Note that our REGEX functions only work with text strings. If you are using either a rollup or lookup field, you may need to first convert the data into a string before you can use a REGEX function on it (you could use ARRAYJOIN() for this). Also note that rollup, lookup, and formula fields do not currently support rich text.

In our formula field reference, you'll find the basic overview for the REGEX() functions that Airtable currently supports. Currently, Airtable supports three functions: REGEX_MATCH(), REGEX_EXTRACT(), and REGEX_REPLACE(). Each of these functions has advantages and disadvantages depending on the formula output you are expecting to be produced in your formula field.

NOTE

In this article we will not be covering all of the Regular Expression patterns in-depth. There are many outside resources that provide great deep dives into REGEX and pattern matching (like regex101,  or the MDN Regex cheatsheet)  that we recommend checking out for more detail.

You may be wondering what a regular expression is; you can think of a regular expression as an arrangement of symbols and characters conveying a string or pattern to be searched for within a larger body of text. The result is something similar to a search engine.

In Airtable, we offer the find bar as well as the Search extension to find certain literal instances of certain strings or patterns. Additionally, we offer the FIND() and SEARCH() functions as well as other relevant text extraction functions in the Text operators and functions section of the formula field reference.

However, the REGEX functions that follow provide a way to search the information in your tables as well as the ability to work with that information in powerful ways.

REGEX_MATCH in Airtable

REGEX_MATCH

The REGEX_MATCH() function returns whether the input text matches a regular expression and outputs in the Boolean data type as a 1 (true) or 0 (false) result. Two examples of where this may be useful are in phone number validation and email validation within your base.

REGEX_MATCH phone number validation

If you need to validate a set of phone numbers against specific criteria, you can use REGEX_MATCH() like in the formula below.

REGEX_MATCH( {Possible Phone Number}, '^(\\+\\d{1,2}\\s?)?\\(?\\d{3}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}$')

This regular expression handles a number of different phone number formats:

  • Country codes

  • Using dashes or spaces as delimiters

  • Parentheses around the area code

  • Using letters instead of numbers

360102835113validatephone.png

REGEX_MATCH email validation

Similar to the other example above, you can use REGEX_MATCH() to validate a list of email addresses as well.

REGEX_MATCH( {Email address}, "(\W|^)[\w.\\-]{0,25}@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}(\W|$)")

This regex handles basic validation for email addresses: Starts with a username that is composed of letters, numbers, or characters like underscore, period or dash. An at-sign (@). A domain name, which is a string of letters, numbers, or characters like underscore, period, or dash, followed by a period, followed by another string of letters, numbers, or characters like underscore, period or dash. Email validation is a very complicated subject, but this regex is a good starting point for most use cases.

1500001823541validateemail.png

REGEX_EXTRACT in Airtable

REGEX_EXTRACT

The REGEX_EXTRACT() function returns the first substring that matches a regular expression. If the REGEX_EXTRACT() function finds no matching instance of the provided regular expression it will return an ERROR.

In the example below, this function is used to extract the domain name from a URL field.

REGEX_EXTRACT - extracting domains from URLs

REGEX_EXTRACT( {URL}, '^(?:https?:\\/\\/)?(?:[^@\n]\+@)?(?:www\\.)?([^:\\/\n?]+)')

URLs start with HTTP or HTTPS, possibly a username followed by an @ symbol, the domain, and then the rest of the URL. Here, we strip off the subdomain if it’s ‘www’, but otherwise preserve it.

extract

REGEX_REPLACE in Airtable

REGEX_REPLACE

The REGEX_REPLACE() function substitutes all matching substrings with a replacement string value. Building off of the example above, if we have a valid phone number, we can normalize it using REGEX_REPLACE() to make it easier to work with in the rest of our Airtable base.

REGEX_REPLACE - normalizing phone numbers

IF({Is Valid Phone Number?}, UPPER(REGEX_REPLACE({Possible Phone Number}, '[^A-Za-z0-9]', '')), ERROR('Invalid phone number'))

If the phone number passed validation, we normalize it by using REGEX_REPLACE() to replace non-alphanumeric characters with an empty string, resulting in just the dialable digits. We also use the UPPER() formula to make the casing consistent. Normalizing phone numbers into a prettier format is an exercise left to the reader.

T() and REPT() in Airtable

T() and REPT()

This content is part of a guided course, which includes the functions listed below that can help expand functionality in your Airtable base. 

T() in Airtable

T()

This function returns the argument if it is text, and otherwise returns blank.

T("Text only") would return the argument, "Text only", while T(42) would return as blank.

REPT() in Airtable

REPT()

This function repeats any referenced string a specified number of times.

For example, REPT("Hi! ", 3) would result in Hi! Hi! Hi!.


Was this article helpful?