Search code examples
htmlregexurlgoogle-sheetsre2

How do I use REGEXTRACT in Google Sheets to isolate the GCLID?


I'm a marketer and Google assigns clicks a Google Click ID (GCLID) on paid traffic. These come through appended to URLs. Sometimes, I need to isolate the value in a Google Sheet using REGEX.

Here's what I've done to extract the GCLID in Sublime Text but it is not yielding the expected result in google sheets:

(?<=gclid=).*?[?=A-Za-z0-9\-\_]{10,100}

100 case sensitive alphanumeric characters is the max for these Ids. They also consist of "-" as well as "_".

I believe this is an effective use of a REGEX positive lookback that yields just the GCLID at the end of the URL query.

The use of a ? in Google Sheets seems to break the expression.


Solution

  • You may use

    =REGEXEXTRACT(A1, "gclid=([\w-]+)")
    

    Note that the REGEXEXTRACT function only returns the captured substring(s) if the capturing group(s) is defined in the regular expression.

    The regex matches

    • gclid= - a literal string
    • ([\w-]+) - Capturing group 1: any one or more letters, digits, _ or - (note the - at the end of a character class does not have to be escaped to be parsed as a literal hyphen).