2 回答

TA貢獻1876條經驗 獲得超6個贊
這會起作用:
^(.+)(?:\.s3[-.].*)$
翻譯:
從字符串的開頭找到通向
.s3.
or的所有內容,.s3-
并將其捕獲到組 #1 中。
您的存儲桶名稱將位于$1
.
請參閱下面的 regex101 鏈接并使用代碼生成器查看 Golang 示例。
https://regex101.com/r/LRvA5F/1

TA貢獻2041條經驗 獲得超4個贊
利用
^(.*?)\.s3\b
見證明。
解釋
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
s3 's3'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
去代碼示例:
package main
import (
"fmt"
"regexp"
)
func main() {
r := regexp.MustCompile(`^(.*?)\.s3\b`)
str := "bucket-name.s3.us-west-2.amazonaws.com"
match := r.FindStringSubmatch(str)
fmt.Println(match[1])
}
- 2 回答
- 0 關注
- 215 瀏覽
添加回答
舉報