Search code examples
regexlooker-studioregexp-replacere2

RegEx - Return pattern to the right of a text string for URL


I'm looking to return the URL string to the right of a specific set of text using RegEx:

URL:

www.websitename/countrycode/websitename/contact/thank-you/whitepaper/countrycode/whitepapername.pdf 

What I would like to just return:

/whitepapername.pdf

I've tried using ^\w+"countrycode"(\w.*) but the match won't recognize countrycode.

In Google Data Studio, I want to create a new field to remove the beginning of the URL using the REGEX_REPLACE function.

Ideally using:

REGEX_REPLACE(Page,......)

Solution

  • The REGEXP_REPLACE function below does the trick, capturing all (.*) the characters after the last countrycode, where Page represents the respective field:

    REGEXP_REPLACE(Page, ".*(countrycode)(.*)$", "\\2")
    

    Alternatively - Adapting the RegEx by The fourth bird to Google Data Studio:

    REGEXP_REPLACE(Page, "^.*/countrycode(/[^/]+\\.\\w+)$", "\\1")
    

    Google Data Studio Report as well as a GIF to elaborate: