Home > Net >  How to regex-match a comma delimited list containing AWS Security Group IDs?
How to regex-match a comma delimited list containing AWS Security Group IDs?

Time:01-14

How can I match a comma delimited list which could be either empty or AWS Security Group Ids e.g. sg-qwe12345,sg-543234aewrfs,sg-12eqwrwer,sg-gjkkh12435?

I tried (^$|sg-[0-9a-z] \,|sg-[0-9a-z] )(?<!\,)$ in the allowed pattern in the cloudformation template but when I am entering a comma at the end and deploy stack the stack creation process start and fails.

Ideally, it should stop and ask me to check as (?<!\,)$ in allowed pattern should not allow the comma at the end.

CodePudding user response:

It makes sense to think about how a "list" is defined:

  • Nothing
  • 1 item
  • Several items, which is 1 item followed by a "delimiter" and more items

or, more formally:

list  = '' | items
items = item | item ',' items
item  = 'sg-' ident
ident = [0-9a-z] 

As a regex, this might look like:

^(sg-[0-9a-z] (,sg-[0-9a-z] )*)?$
  • Related