I need to use regular expressions in my Terraform code. The documentation for the replace function says the string if wrapped in a forward slash can be treated as a regex.
I've tried the following:
Name = "${replace(var.string, var.search | lower(var.search), replace)}"
I need to use regex to replace either the string or the lower case of the string with the replace string.
Terraform uses the RE2 regular expression language. This engine does not support all of the features found in some other regular expression engines; in particular, it does not support backreferences.
The regular expression functions identify precise patterns of characters and are useful for data validation, for example, type checks, range checks, and checks for characters that are not allowed. The supported regular expression functions are fully Perl v5 compatible.
» can Function can evaluates the given expression and returns a boolean value indicating whether the expression produced a result without any errors. This is a special function that is able to catch errors produced when evaluating its argument.
» replace Functionreplace searches a given string for another given substring, and replaces each occurrence with a given replacement string. If substring is wrapped in forward slashes, it is treated as a regular expression, using the same pattern syntax as regex .
Just to help someone else looking here... following the Terraform documentation: https://www.terraform.io/docs/language/functions/replace.html
To be recognized as a Regex, you need to put the pattern between / (slashes), like this:
> replace("hello world", "/w.*d/", "everybody")
> hello everybody
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With