Yogi
Yogi

Reputation: 1047

Terraform regular expression to extract part of url

I have a url like

postgres://some-url.com:23244/users-pool?sslmode=require

I basically need to match everything between // and : . So in this case I need some-url.com. I am trying this regular expression /(?<=\/\/)(.*?)(?=\:)/gm and it works on online regex tools. Howeever when I try to do this on TF

regex("postgres://some-url.com:23244/users-pool?sslmode=require", "(?<=//)(.*?)(?=\:)")

I am getting

│
│   on <console-input> line 1:
│   (source code not available)
│
│  Error: Invalid escape sequence
│
│   on <console-input> line 1:
│   (source code not available)
│
│ The symbol "/" is not a valid escape sequence selector.
╵

╷
│ Error: Invalid escape sequence
│
│   on <console-input> line 1:
│   (source code not available)
│
│ The symbol "/" is not a valid escape sequence selector.```


How can I do this on Terraform? Appreciate the help

Upvotes: 1

Views: 2027

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

If it should be between // and the first occurrence of : you can use a negated character class excluding matching the colon in between:

//([^:]*):

See a regex101 demo.

Upvotes: 0

Marcin
Marcin

Reputation: 238081

Pattern should be first, not second:

regex("//(.*):", "postgres://some-url.com:23244/users-pool?sslmode=require")

Upvotes: 1

Related Questions