由于Google Big Query中的正则表达式导致Python错误 [英] Python error because of regex inside a Google Big Query
问题描述
我正在用python编写Google Big Query包装器.其中一个查询具有正则表达式,而python代码将其视为语法错误.
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.
这是正则表达式当tier2包含'-'则REGEXP_EXTRACT(tier2,'(.*)\ s-')
Here is the regex WHEN tier2 CONTAINS '-' THEN REGEXP_EXTRACT(tier2,'(.*)\s-')
错误是无效的字符串文字:'(.*)\ s-'>错误是针对正则表达式中的\.任何克服它的建议
the error is Invalid string literal: '(.*)\s-'> The error is for \ in the regex. Any suggestion to overcome it
推荐答案
您需要在反斜杠之前加上另一个反斜杠来进行反斜杠
反斜杠 \
是转义字符,因此您需要对其进行转义,以便将其视为普通字符
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
尝试
'(.*)\\s-'
根据您的评论,看起来上面正是您在BigQuery中使用的内容-因此,在这种情况下,您需要转义两个反斜杠
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-'
这篇关于由于Google Big Query中的正则表达式导致Python错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!