gpt4 book ai didi

regex - 如何正则表达式匹配包含 AWS 安全组 ID 的逗号分隔列表?

转载 作者:行者123 更新时间:2023-12-03 07:22:10 34 4
gpt4 key购买 nike

如何匹配逗号分隔的列表,该列表可以为空或 AWS 安全组 ID,例如sg-qwe12345,sg-543234aewrfs,sg-12eqwrwer,sg-gjkkh12435

我试过(^$|sg-[0-9a-z]+\,|sg-[0-9a-z]+)(?<!\,)$在 cloudformation 模板中允许的模式中,但是当我在末尾输入逗号并部署堆栈时,堆栈创建过程开始并失败。

理想情况下,它应该停止并要求我检查为 (?<!\,)$在允许的模式中不应允许末尾有逗号。

最佳答案

思考如何定义“列表”是有意义的:

  • 没什么
  • 1 项
  • 多个项目,即 1 个项目后跟一个“分隔符”和多个项目

或者,更正式地说:

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

作为正则表达式,这可能看起来像:

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

但是既然您使用的是 AWS CloudFormation,为什么不选择合适的类型,例如 CommaDelimitedList ?使用这种类型,正则表达式可以大大简化为 ^(sg-[0-9a-z]+)$;类型本身负责其余部分(逗号处理)。

来自文档:

AllowedPattern: […]. When applied to a parameter of type CommaDelimitedList, the pattern must match each value in the list.

CommaDelimitedList: An array of literal strings that are separated by commas. The total number of strings should be one more than the total number of commas. Also, each member string is space trimmed.

For example, users could specify "test,dev,prod", and a Ref would result in ["test","dev","prod"].

您是否提供了ConstraintDescriptionDefault值?

关于regex - 如何正则表达式匹配包含 AWS 安全组 ID 的逗号分隔列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75096372/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com