Sawadi
Sawadi

Reputation: 111

Terraform GCP Unsupported escape sequence in regex

I am trying to set up custom metrics in GCP using terraform code. I have a problem with the label extractor part to get 4xx and 5xx response codes. Basing on console response and Google builder I managed to create this regex:

\\s([4-5][0-9][0-9])\\s\ 

googleapi: Error 400: Failed to parse extractor expression: unsupported escape sequence in a string literal at line 1, column 36, token '"\s([1-5][0-9][0-9])\s"'

extracted_label = "REGEXP_EXTRACT(jsonPayload.message, \"\\s([1-5][0-9][0-9])\\s\")"

I would like to be able to create a metric, but I cannot bypass the unsupported escape sequence problem. I would be grateful for any help.

Upvotes: 2

Views: 1929

Answers (2)

Anton Golubev
Anton Golubev

Reputation: 1433

Leaving here another example for somebody trying to code regex extractors in Terraform:

Original regex expression:

/analysis/([^\/?]+)

In Terraform:

endpoint    = "REGEXP_EXTRACT(jsonPayload.message, \"/analysis/([^\\\\/?]+)\")"

Spent one hour trying to make a regex 'right.' Embedded formats are evil.

Upvotes: 0

Sawadi
Sawadi

Reputation: 111

I managed to find answer by myself to this question. The correct way is using 4 slashes \\\\ Fixed code line should look like this:

extracted_label = "REGEXP_EXTRACT(jsonPayload.message, \"\\\\s([4-5][0-9][0-9])\\\\s\")" 

Also, if someone would look for sample code in Terraform GCP to pull HTTP response code from readiness health check here is one:

extracted_label = "REGEXP_EXTRACT(jsonPayload.message, \"\\\\w+\\\\/\\\\d\\\\.\\\\d\\\"\\\\s([4-5][0-9][0-9])\")"

Upvotes: 9

Related Questions