Search code examples
pythonregexgoogle-analytics-api

Regex in Google Analytics v4 to fetch pagePath not containing "?"


I want to fetch the pagePath dimension which does not contain the symbol ? in Google Analytics Reporting API v4 Python client.

The problem is, that all the solutions on the internet suggest using "^((?!\?).)*$" (a negative lookahead). This solution works fine elsewhere, except the Google Analytics Reporting API. As I'm getting the following error:

HttpError: <HttpError 400 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "Invalid regular expression: '^((?!\?).)*$'. Regular expressions should follow RE2 syntax.">

And I'm unable to find a solution to this. Please mention if I'm doing something wrong, and the correct solution if possible.


Solution

  • The pattern ^((?!\?).)*$ uses a negative lookahead which matches any char except a newline when what is directly on the right is not a ? and that syntax is not supported by re2.

    You might simplify that to using a negated character class matching any char 1+ times (to prevent matching an empty string) except a ? or a newline.

    ^([^?\r\n]+)$
    

    Regex demo

    If you want the match only you can omit the parenthesis.