stringazureterraformhcl

Terraform string manipulation remove and add sub-strings


I have a sting

crdemo01.australiaeast.data.azurecr.io

I want to build the following string from the above one:

crdemo01-test.australiaeast.data

The same logic should apply to the following input strings as well:

crdemo01.australiasoutheast.data.azurecr.io
crdemo01.azurecr.io

What I'm trying to achieve is to remove the .azurecr.io part from the input string and append -test to the first part of the remaining string.

How can I achieve this in Terraform?


Solution

  • Using replace twice with regex patterns:

    > replace(replace("crdemo01.australiaeast.data.azurecr.io","/^[^.]+/","$0-test"),"/.azurecr.io$/","")
    "crdemo01-test.australiaeast.data"