REGEXP_EXTRACT Examples in Google Data Studio

Figuring out the correct regular expression is by far the most difficult part of using formulas in Google Data Studio. Sometime in the future, I’ll write a longer post on just that. Today I wanted to get you started with some examples and case studies to start trimming your data for your dashboards.

The function REGEXP_EXTRACT creates a dimension by extracting a subset of an already available source dimension. Google uses Google RE2 regular expressions, so it’s worth a read to get a handle on how to use regular expressions in Google Data Studio.

If you still don’t understand REGEX after reading that link, keep trying. I’ve read it 5 times and I still don’t have a full grasp like I should.

Example #1 – Remove Website Name from Title with REGEXP_EXTRACT

Let’s assume your website is like mine and the page title includes the name of your website. The “Data Studio Help” at the end of each line just gets in the way and doesn’t add value.

REGEXP_EXTRACT

To remove that, click “Add Dimension” then “Create Field”. Copy the below code into the formula box and hit “Apply”.

REGEXP_EXTRACT(Page Title,’^(.+?)-‘)

REGEXP_EXTRACT

Example #2 – Remove Subcategory of Affinity Category with REGEXP_EXTRACT

To remove the sub category in the Affinity Category, you would use the below REGEX. The “(/|&)” searches for “/” OR “&” and removes everything that follows.

REGEXP_EXTRACT(Affinity Category (reach),’^(.+?)(/|&)’)

REGEXP_EXTRACT

If you removed the original Affinity Category from the chart, Google combines the metrics under your new categories.

REGEXP_EXTRACT Example
REGEXP_EXTRACT Example

Example #3 – Find First Sub-folder of URL with REGEXP_EXTRACT

If you don’t have Enhanced Ecommerce set up on your site or if you’re not running a commerce site. This particular example will be useful. The below formula extracts the first sub folder in your URL.

REGEXP_EXTRACT(Landing Page,'([^:/?\n]+)’)

REGEXP_EXTRACT Example

If you removed the original Landing Page from the chart, Google combines the metrics under your new folder structure. This way, you can see what sections of your site is driving the most users.

REGEXP_EXTRACT Example
REGEXP_EXTRACT Example

Other Text Functions in Google Data Studio


When you finally decide to get smarter about all things digital and get your first second income stream built to make that WiFi Money. You need to sign up for my Substack below. Start with the article on Your Path to WiFi Money.

If you're looking at how to start your own website, I've created an easy guide for that too.