简体   繁体   English

Terraform 正则表达式提取部分 url

[英]Terraform regular expression to extract part of url

I have a url like我有一个 url 之类的

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 .所以在这种情况下,我需要some-url.com I am trying this regular expression /(?<=\/\/)(.*?)(?=\:)/gm and it works on online regex tools.我正在尝试这个正则表达式/(?<=\/\/)(.*?)(?=\:)/gm并且它适用于在线正则表达式工具。 Howeever when I try to do this on TF但是,当我尝试在 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

Pattern should be first , not second: Pattern 应该是first ,而不是 second :

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

If it should be between // and the first occurrence of : you can use a negated character class excluding matching the colon in between:如果它应该在//和第一次出现的:之间,您可以使用否定字符 class 排除匹配之间的冒号:

//([^:]*):

See a regex101 demo .请参阅regex101 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM