Home > database >  Writing using Regular Expressions in Google Cloud query
Writing using Regular Expressions in Google Cloud query

Time:12-03

I currently have a regular expression: REGEXP_EXTRACT_ALL(data, r'\"createdAt\"\:(.*?)\}') Which finds "createAt":" and outputs anything past that text and up until the next "}".

Example output: {"_seconds":1620327345,"_nanoseconds":155071000

This works BUT I need the last } to be included in the output.

Preferred Output: {"_seconds":1620327345,"_nanoseconds":155071000}

How will I need to change my regular expression so that the } is included in the output?

CodePudding user response:

You need to include the } into the capturing group:

REGEXP_EXTRACT_ALL(data, r'"createdAt":(.*?})')

Besides, you can make it a bit more efficient with a negated character class:

REGEXP_EXTRACT_ALL(data, r'"createdAt":([^}]*})')

With [^}]*, you match any zero or more chars other than } as many times as possible.

Also, if you chose single quotation marks as a string literal delimiter char, you need not escape double quotation marks (they are not special regex metacharacters.) Note } is not a special character if there is no paired { with a number (or {<number>,<number>) in front of it.

  • Related