2

from google analytics export I am trying to extract last number from the trafficSource.campaign field with next query:

SELECT
 REGEXP_EXTRACT(trafficSource.campaign,r':(\d+$)') as campaign,
FROM
  [95677969.ga_sessions_20160109] AS results,
WHERE
  hits.type IN ('TRANSACTION','PAGE')

This Query works fine in UI and the number is returned by every row match the reg expresion, but when I copy paste this query in my script allways null value is returned.

trafficSource.campaign values are like this:

_dfa_107202:4637224:8531522

Please can anybody help me?

2
  • Can you please publish your script here? Commented Feb 9, 2016 at 15:52
  • Thank you for your comment @VadimSolovey, the problem was with the regex engin, it does´nt accept re2 perl character classes. Commented Feb 10, 2016 at 16:53

1 Answer 1

3

Escaping is the problem here.

When you give BigQuery something like:

SELECT COUNT(*) FROM [publicdata:samples.shakespeare] WHERE REGEXP_MATCH(word, r'^\w$')

That will work fine in the BigQuery UI (283 is the result). But if you copy the same string into your favorite programming language, it will probably try to read the \ as one of its escape characters, so you will have to double escape it - or figure an alternative way to feed it the string unaltered by your script interpreter.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Felipe, you are right, the problem is in the regex engine and it works with r'([[:digit:]]{3,}$)'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.