johnnyrocket33
johnnyrocket33

Reputation: 131

Multi match in re2 regex for terraform

I'm trying to apply regex in something like this, in terraform.

variable "version" {
   type        = "string"
   default     = https://s3-us-east-1.amazonaws.com/bucket/folder/item-v1.2/item-item2-v1.2.gz
   description = "version"
}

name = "${replace(var.version, "//", "")}"

I just need to replace everything and output only "v1.2" for the name, specifically from the item-v1.2 file path. I was thinking of doing something along the lines of

name = "${replace(var.version, "//", "")}"

wherein I replace everything but v1.2.

Any idea how to do it? Thank you in advance!

Upvotes: 1

Views: 577

Answers (1)

yvesonline
yvesonline

Reputation: 4837

There are a couple of issues with your variable definition (e.g. wrong use of quotes, variable name "version" is reserved due to its special meaning inside module blocks, and so on) so I operate on the following:

variable "my_version" {
    type        = string
    default     = "https://s3-us-east-1.amazonaws.com/bucket/folder/item-v1.2/item-item2-v1.2.gz"
    description = "version"
}

Then you can extract the version (or replace everything else except the version as requested) from the sub-folder path like this (this has been run and tested with terraform console):

$ terraform console
> var.my_version
"https://s3-us-east-1.amazonaws.com/bucket/folder/item-v1.2/item-item2-v1.2.gz"
> regex(".*-(.*)/.*.gz", var.my_version)[0]
"v1.2"
> replace(var.my_version, "/.*-(.*)/.*.gz/", "<before> $1 <after>")
"<before> v1.2 <after>"

Upvotes: 1

Related Questions