Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions in Terraform

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.

like image 629
Sam Avatar asked Mar 15 '17 11:03

Sam


People also ask

Does terraform support regex?

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.

What are regex functions?

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 you function in terraform?

» 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.

How do you replace a string in terraform?

» 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 .


1 Answers

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
like image 200
Ualter Jr. Avatar answered Sep 20 '22 13:09

Ualter Jr.