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

I have a list of GCP services, e.g. 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 = [
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        = [

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:

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

1 Like