The symbol "." is not a valid escape sequence selector

I have a list of GCP services, e.g. monitoring.googleapis.com. I want to transform this list into a new list that contains a regex for each entry , e.g. monitoring\.googleapis\.com/.? but I get the following error.

The symbol “.” is not a valid escape sequence selector.

variable "project_services" {
  type = list(string)
  default = [
    "monitoring.googleapis.com",
    "stackdriver.googleapis.com",
    "compute.googleapis.com",
    "bigquery.googleapis.com"
  ]
}
output "new_list"{
  value = [for enabled_api in var.project_services: "${replace(enabled_api, ".", "\.")}/.?" ]
}

How can I get around this error?

Using value = [for enabled_api in var.project_services: "${replace(enabled_api, ".", "\\.")}/.?" ] results in the below which is wrong.

new_list        = [
     "monitoring\\.googleapis\\.com/.?",
     "stackdriver\\.googleapis\\.com/.?",
     "compute\\.googleapis\\.com/.?",
     "bigquery\\.googleapis\\.com/.?",
 ]

I don’t think it is wrong. Not if it’s being represented in proper Terraform syntax.

A single literal backslash needs to be shown/written as two because that’s part of the syntax: https://www.terraform.io/language/expressions/strings#escape-sequences

It’ll be a single backslash after the string parser has dealt with it, before it gets to the regex engine.

1 Like