1

I am writing Google Big Query wrappers in python. One of the queries has a regex and the python code is treating it as an syntax error.

Here is the regex WHEN tier2 CONTAINS '-' THEN REGEXP_EXTRACT(tier2,'(.*)\s-')

the error is Invalid string literal: '(.*)\s-'> The error is for \ in the regex. Any suggestion to overcome it

1 Answer 1

2

You need to escape backslash by preceding it with yet another backslash
Backslash \ is an escape character so you need to escape it so it is treated as a normal character

Try

'(.*)\\s-'  

Based on your comments, looks like above is exactly what you are using in BigQuery - so in this case you need to escape each of two backslashes

'(.*)\\\\s-'  
Sign up to request clarification or add additional context in comments.

4 Comments

you are right...the code works in big query with \\ but in python somehow its throwing error.
This is what i am using in the query .....WHEN tier2 CONTAINS '-' THEN REGEXP_EXTRACT(tier2,'(.*)\\s-')
so you need escape it twice in this case - try '(.*)\\\\s-'
You are amazing...Mr Berlyant you are brilliant :)

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.