How do I use replace() with '//' without invoking Regex behavior?

1,394 views
Skip to first unread message

Ethan Edwards

unread,
Apr 26, 2016, 2:18:33 PM4/26/16
to Terraform
I'm trying to use replace() to replace two forward slashes '//' with a single slash '/'.

However, if you wrap the 'search' argument in forward slashes, it treats it as a regex.

This is a problem because I'm trying to use '//' as the value of the search argument, as a literal.

If I try and escape it using backslashes, eg. '\/\/', I get an 'illegal char escape' error.

Is there a workaround? I don't like the fact that terraform automatically assumes regex based on the content, rather than letting me specify 'true' as a fourth argument for regex parsing or something similar.

Thanks,

-Ethan

path_prefix = "${replace(concat(var.consul_prefix, "/"), "//", "/")}${var.nlog_config_name}/"


Ethan Edwards

unread,
Apr 26, 2016, 2:22:15 PM4/26/16
to Terraform
I also tried using a regex search itself to do the replace, still fails validation with "illegal char escape":

path_prefix = "${replace(concat(var.consul_prefix, "/"), "/\/\//", "/")}${var.nlog_config_name}/"

This theoretically should be doing a regex search "/ /" for the regex value "\/\/" which is two forward slashes, escaped.
Reply all
Reply to author
Forward
0 new messages