Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of r in REGEXP_EXTRACT(word,r'(\w\w\'\w\w)')

I can't find an answer in either BigQuery Reference or re2 wiki.

In all the examples for Regex section in the BigQuery Reference there is an 'r' before every regex, but I can't find anywhere the meaning of it. For example:

REGEXP_EXTRACT(word,r'(\w\w\'\w\w)')

It seems to me as a typecast of 'regex' or something, because it allows for '\' and ''' in the following string.

Thanks to anyone who knows and posts an answer to this :)

like image 399
Antonín Hoskovec Avatar asked Dec 12 '22 02:12

Antonín Hoskovec


1 Answers

From Wikipedia "A few languages provide a method of specifying that a literal is to be processed without any language-specific interpretation. This avoids the need for escaping, and yields more legible strings."

http://en.wikipedia.org/wiki/String_literal#Raw_strings

For example, this looks like a valid regex, but the escapes get miss-interpreted:

SELECT REGEXP_EXTRACT("ab'cd", '(\w\w\'\w\w)') 

Error: Invalid string literal: '(\w\w\'\w\w)'

I can fix this in two ways. With a raw string, or escaping the escapes:

Escaping the escapes:

SELECT REGEXP_EXTRACT("ab'cd", '(\\w\\w\'\\w\\w)')
ab'cd

Raw string:

SELECT REGEXP_EXTRACT("ab'cd", r'(\w\w\'\w\w)')
ab'cd
like image 69
Felipe Hoffa Avatar answered Jan 09 '23 04:01

Felipe Hoffa