亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用正則表達式從 aws s3 url 中提取存儲桶名稱

使用正則表達式從 aws s3 url 中提取存儲桶名稱

Go
智慧大石 2022-07-11 15:21:34
我想從 AWS s3 URL 中提取存儲桶名稱。URL 可以有多種格式。以下是支持的 s3 URL 的正則表達式列表:[a-z0-9.-]+\.s3\.amazonaws\.com[a-z0-9.-]+\.s3-[a-z0-9-]+\.amazonaws\.com[a-z0-9.-]+\.s3\.[a-z0-9-]+\.amazonaws\.com[a-z0-9.-]+\.s3-website[.-](eu|ap|us|ca|sa|cn)例子:bucket-name.s3.us-west-2.amazonaws.combucket.name.s3.us-west-2.amazonaws.combucket-name.s3-us-west-2.amazonaws.combucket.name.s3-us-west-2.amazonaws.combucket-name.s3.amazonaws.combucket.name.s3.amazonaws.com我想要一個可以bucket-name從GoLang.
查看完整描述

2 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

這會起作用:

^(.+)(?:\.s3[-.].*)$

翻譯:

從字符串的開頭找到通向.s3.or的所有內容,.s3-并將其捕獲到組 #1 中。

您的存儲桶名稱將位于$1.

請參閱下面的 regex101 鏈接并使用代碼生成器查看 Golang 示例。

https://regex101.com/r/LRvA5F/1


查看完整回答
反對 回復 2022-07-11
?
縹緲止盈

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])

}


查看完整回答
反對 回復 2022-07-11
  • 2 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號