Home > Software engineering >  Terraform regular expression to extract part of url
Terraform regular expression to extract part of url

Time:02-03

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

CodePudding user response:

Pattern should be first, not second:

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

CodePudding user response:

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.

  • Related