hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.73k stars 9.56k forks source link

regex lookbehind lookahead #28846

Open yaitskov opened 3 years ago

yaitskov commented 3 years ago

I would like to have support for look-ahead / look-behind.

locals { mimes = jsondecode(file("${path.module}/mimes.json")) }
lookup(local.mimes, regex("(?<=[.])[^.]+$", "file.sh"), null)

Having look-behind feature I could avoid prefixing file extensions with dots.

{
 ".txt": "text/plain",
 ".html": "text/html", 
 ".sh": "text/x-shellscript"
}
jbardin commented 3 years ago

Hi @yaitskov,

For context, Terraform, the upstream go-cty package which the language is based on, and the underlying Go language standard library all use an implementation of RE2 which purposely doesn't support the non-regular features of some regex engines. We don't have any plans to implement or support a PCRE regex engine for Terraform.

Most regex actions that require lookahead and lookbehind can be accomplished in other ways, the example here being that you can split the values on the . character: split(".", "file.sh")[1].

richardj-bsquare commented 1 year ago

To get around this I used an external data source to run a simple python re match() script, which is kinda clunky but works if you can calculate all potential regular expressions before hand and run the data source over a map of possible values.

My particular use case was to match a particular group of strings with a regex, but exclude a subset of those that would match, all specified by an external YAML file. Due to the variability of the YAML file the use of negative lookahead (?!) was required as I couldn't code my way out of it in terraform (which is my usual solution).

Such a shame, as it's just another one of those things that gets in the way of writing elegant terraform (lack of lambdas or other self-defined functions being another).

crw commented 8 months ago

Thank you for your continued interest in this issue.

Terraform version 1.8 launches with support of provider-defined functions. It is now possible to implement your own functions! We would love to see this implemented as a provider-defined function.

Please see the provider-defined functions documentation to learn how to implement functions in your providers. If you are new to provider development, learn how to create a new provider with the Terraform Plugin Framework. If you have any questions, please visit the Terraform Plugin Development category in our official forum.

We hope this feature unblocks future function development and provides more flexibility for the Terraform community. Thank you for your continued support of Terraform!