Reputation: 623
I have a regex to replace any non word characters, underscores or spaces in one of my variables to sanitise the name to pass it to Route53.
It is replacing non alphanumeric and underscores but it is getting stuck on spaces.
I know the regex is \s
for whitespaces but it seems to be ignoring it when the Terraform plan runs but if I run it through a regex checker it works fine.
identifier = "qa-${lower(replace(var.dns_name,"/\\W|_|\\s/","-"))}"
Upvotes: 0
Views: 8473
Reputation: 2401
I used your regex in Terraform v0.12.21 and tested it with spaces and various special characters:
dns_name = "abc DEF 123 ~'`!@#$%^&*()_+-=[]\\{}|;':\\./<>?\"\\'.com"
identifier = "${lower(replace(var.dns_name,"/\\W|_|\\s/","-"))}"
and terraform plan
showed that the regex correctly replaced anything other than letters, numbers, or hyphens with hyphens, which seems to be a what you needed for Route 53:
"abc-def-123-------------------------------------com"
But also be aware that domain names might not be able to contain two consecutive hyphens (see here).
Upvotes: 4