2
I’m writing a code in Google App Script using the Google Analytics API where I make a request using a regex filter. A similar example is written below:
var id = xxxxxxx;
var startDate = '2019-03-01';
var endDate = '2019-03-26';
var metrics = 'ga:uniquepageviews';
var options = {
'dimensions':'ga:pagepath',
'filters':'ga:pagepath=~^\/maquiagem($|\?|\/($|\?|.*))'
};
var report = Analytics.Data.Ga.get(id, startDate, endDate, metrics, options);
the point is that when I run this code Appscript returns me the following error:
"Failed API call to Analytics.data.ga.get with Invalid error regular Expression: ' /makeup($|?|/($|?|.*)'. Regular Expressions should follow RE2 syntax."
I did several tests with the call including and taking out parts of the regex to try to identify where the program was rejecting the expression and what all indicates the problem is in the question ("?") within the parentheses (when I take it out of parentheses it is no problem). But of course, I need the interrogation to stay.
I also looked for the RE2 syntax, that the error speaks, but I was not successful in understanding what the problem.
Someone could help me by returning a possible solution to use a regex that is equivalent to this and that the Google API accepts?
Have you tried using the interrogation as
?
only instead of\?
?– CypherPotato
Yes, I have tried and I made the same mistake. But even if it had worked, I need regex to match with literal interrogation.
– WoolfDall
Try placing two inverted bars instead of one in the escapes. For example:
ga:pagepath=~^\/maquiagem($|\\?|\\/($|\\?|.*))
– Sam
It worked! Can you explain the logic of the two backslashes?
– WoolfDall
Inside a string a is usually used for escape sequences, such as the
\n
which is a line break (is a single character), the\t
for TAB, etc. If you want the character itself, you have to write (which is interpreted as only one ). It’s a common problem when you have to put a regex inside a string: https://stackoverflow.com/a/33582461– hkotsubo
Another detail is that
$
means "string end". If you want the character itself$
, will have to put the backslash before it too :-)– hkotsubo
The @hkotsubo explained above =]
– Sam