0

I'm looking for a regex with the following possibilities where for a field it allows only Account Id/ARN (one of the following):

  1. AWS account ID: 12 digit number
  2. AWS account ARN: FORMAT: arn:aws:iam::AWS-account-ID:root.
    Here AWS-Account-ID is a 12 digit number
  3. IAM user ARN: FORMAT: arn:aws:iam::AWS-account-ID:user/user-name-1.
    Here AWS-Account-ID is a 12 digit number and user, user-name-1 are alphanumeric.

The field is a Comma seperated list which may have one or more values.

jatinderjit
  • 1,359
  • 8
  • 21

1 Answers1

2
  1. Tokenize it using the comma.
  2. For each token:
    1. Test if it is 12 length, and contains only numbers (you could use regex for this, but there really is no need: ^\d{12}$ )
    2. Else, test using the regex: ^arn:aws:iam::\d{12}:(?:root|user\/[A-Za-z0-9]+)$

You could regex the whole thing... ^((?:\d{12}|arn:aws:iam::\d{12}:(?:root|user\/[A-Za-z0-9]+)),?\s*)*

You can also use variables in the regex rather than substituting for hard-code, obviously.

For the user name, add any other legal characters to the character class [A-Za-z0-9] e.g. for underscores: [A-Za-z0-9_]. You can also provide a minimum and maximum length, e.g. min 5, max 15: [A-Za-z0-9]{5,15}.

Impact
  • 141
  • 5
  • Can you provide an example of how do i seperate those multiple options with the comma? For now, the regex that you provided takes 13 digits as well –  Jun 24 '18 at 11:11
  • I do not know the language you are using. It's extremely easy to find out how to do it, though. e.g. google "java split string" and you find this https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java As for the regex, `\d{min,max}` captures any digit between min and max times. If you do not specify a max, it will just look for the minimum, fixed amount. `\d{12}` can only match 12 digits. – Impact Jun 24 '18 at 13:29