正则表达式可以匹配多种格式以提取 AWS 账户 ID。

huangapple go评论102阅读模式
英文:

Regex that match multiple format to extract aws accountid

问题

我想使用一个正则表达式来提取两种格式的 AWS 账户 ID:

  1. arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a
  2. 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

  1. arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a
  2. 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:,后面跟着 ec2iam,然后是一个可能为空的段落。在前缀之后必须有一个非空的数字组。这个组就是实例号。

示例:https://go.dev/play/p/mP6VyIYa3v_N

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. const (
  7. s1 = "arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a"
  8. s2 = "arn:aws:iam::1234567890:instance-profile/aws-elasticbeanstalk-ec2-role"
  9. )
  10. func main() {
  11. reStr := `arn:aws:(?:ec2|iam):[^:]*:([0-9]+):`
  12. re, err := regexp.Compile(reStr)
  13. if err != nil {
  14. panic(err)
  15. }
  16. m1 := re.FindStringSubmatch(s1)
  17. fmt.Println("ec2中的用户ID:", m1[1])
  18. m2 := re.FindStringSubmatch(s2)
  19. fmt.Println("iam中的用户ID:", m2[1])
  20. }

这个正则表达式使用了非捕获组 (?: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

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. const (
  7. s1 = "arn:aws:ec2:us-west-2:1234567890:instance/i-0a89fdbedc3f1d76a"
  8. s2 = "arn:aws:iam::1234567890:instance-profile/aws-elasticbeanstalk-ec2-role"
  9. )
  10. func main() {
  11. reStr := `arn:aws:(?:ec2|iam):[^:]*:([0-9]+):`
  12. re, err := regexp.Compile(reStr)
  13. if err != nil {
  14. panic(err)
  15. }
  16. m1 := re.FindStringSubmatch(s1)
  17. fmt.Println("User id in ec2: ", m1[1])
  18. m2 := re.FindStringSubmatch(s2)
  19. fmt.Println("User id in iam: ", m2[1])
  20. }

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

不需要正则表达式,只需提取第五列:

  1. package aws
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. func extract(s string) (int, error) {
  7. split := strings.SplitN(s, ":", 6)
  8. return strconv.Atoi(split[4])
  9. }

https://godocs.io/strings#SplitN

英文:

Dont need RegExp, just pick out the fifth column:

  1. package aws
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. func extract(s string) (int, error) {
  7. split := strings.SplitN(s, ":", 6)
  8. return strconv.Atoi(split[4])
  9. }

https://godocs.io/strings#SplitN

huangapple
  • 本文由 发表于 2022年10月1日 09:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/73915254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定