英文:
Regex that match multiple format to extract aws accountid
问题
我想使用一个正则表达式来提取两种格式的 AWS 账户 ID:
arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a
arn:aws:iam::1234567890:instance-profile/aws-elasticbeanstalk-ec2-role
也就是说,这个正则表达式应该能够在这两行中匹配到 1234567890
。
我知道如何为每种格式创建正则表达式,但如果可能的话,我希望有一个单独的正则表达式。
英文:
I want to use one regex to extract the aws account id for two types of format
arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a
arn:aws:iam::1234567890:instance-profile/aws-elasticbeanstalk-ec2-role
I.e., the regex should be able to match 1234567890
in both lines.
I know how to create regex for each format, but want to have one single regex if possible.
答案1
得分: 0
我会使用以下正则表达式进行翻译:arn:aws:(?:ec2|iam):[^:]*:([0-9]+):
前缀是 arn:aws:
,后面跟着 ec2
或 iam
,然后是一个可能为空的段落。在前缀之后必须有一个非空的数字组。这个组就是实例号。
示例:https://go.dev/play/p/mP6VyIYa3v_N
package main
import (
"fmt"
"regexp"
)
const (
s1 = "arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a"
s2 = "arn:aws:iam::1234567890:instance-profile/aws-elasticbeanstalk-ec2-role"
)
func main() {
reStr := `arn:aws:(?:ec2|iam):[^:]*:([0-9]+):`
re, err := regexp.Compile(reStr)
if err != nil {
panic(err)
}
m1 := re.FindStringSubmatch(s1)
fmt.Println("ec2中的用户ID:", m1[1])
m2 := re.FindStringSubmatch(s2)
fmt.Println("iam中的用户ID:", m2[1])
}
这个正则表达式使用了非捕获组 (?:ec2|iam)
。与该组对应的子匹配不包含在 regexp.FindStringSubmatch
返回的数组中。这样做只是为了方便:FindStringSubmatch
返回的数组只有两个元素 - 完全匹配和组 ([0-9]+)
的匹配。
你也可以使用完整的组 (ec2|iam)
。在这种情况下,用户ID组在 FindStringSubmatch
返回的匹配数组中的索引为 2
。
英文:
I'd use the following regexp: arn:aws:(?:ec2|iam):[^:]*:([0-9]+):
The prefix is arn:aws:
, followed by either ec2
or iam
and one may-be-empty segment. After the prefix there must be a non-empty group of digits. It is exactly the group with the instance number.
Example https://go.dev/play/p/mP6VyIYa3v_N
package main
import (
"fmt"
"regexp"
)
const (
s1 = "arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a"
s2 = "arn:aws:iam::1234567890:instance-profile/aws-elasticbeanstalk-ec2-role"
)
func main() {
reStr := `arn:aws:(?:ec2|iam):[^:]*:([0-9]+):`
re, err := regexp.Compile(reStr)
if err != nil {
panic(err)
}
m1 := re.FindStringSubmatch(s1)
fmt.Println("User id in ec2: ", m1[1])
m2 := re.FindStringSubmatch(s2)
fmt.Println("User id in iam: ", m2[1])
}
This regexp uses non-capturing group (?:ec2|iam)
. The submatch that corresponds to this group is not included in the array returned by regexp.FindStringSubmatch
. This is done for conveniece only: the array returned by FindStringSubmatch
has only 2 elements - the full match and the match for the group ([0-9]+)
.
You can use full group (ec2|iam)
. In this case the User ID group will have index 2
in the array of matches returned by FindStringSubmatch
.
答案2
得分: 0
不需要正则表达式,只需提取第五列:
package aws
import (
"strconv"
"strings"
)
func extract(s string) (int, error) {
split := strings.SplitN(s, ":", 6)
return strconv.Atoi(split[4])
}
https://godocs.io/strings#SplitN
英文:
Dont need RegExp, just pick out the fifth column:
package aws
import (
"strconv"
"strings"
)
func extract(s string) (int, error) {
split := strings.SplitN(s, ":", 6)
return strconv.Atoi(split[4])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论