Search code examples
google-analyticsregex-negationregular-languagelooker-studio

How can I extract the last path of an URL in Google Data Studio custom fields?


I'm using REGEXP_EXTRACT to get the last path of the Page Dimension in Google Data Studio. The Page URL has one identifier which is the same on all possible URLs:

+------------------------------------------------------+
|                         Page                         |
+------------------------------------------------------+
| /ABC/something1/something2/something3/lastpath       |
| /ABC/something1/something2/something3/last path      |
| /ABC/something1/something2/something3/last-path      |
| /ABC/something1/something2/something3/last last-path |
+------------------------------------------------------+

So I want to extract the last path behind /something3/, all the values in the last directory.

Here is what I got:

REGEXP_EXTRACT(Page,'/ABC/([^/]+/){3}') 

But this matches only the /something3/ directory.


Solution

  • Idea is to use capturing groups only for something you want extract. For everything else use the non-capturing (?:REGEX).

    SELECT REGEXP_EXTRACT(
        '/ABC/something1/something2/something3/last last-path',
        '/ABC/(?:[^/]+/){3}(.+)');