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

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

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:,后面跟着 ec2iam,然后是一个可能为空的段落。在前缀之后必须有一个非空的数字组。这个组就是实例号。

示例: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])
}

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:

确定