如果你想要一个正则表达式,这是我对此的看法:
^ # match start of string
(?=.*a(?:,|$)) # assert it matches a followed by a comma or end-of-str
(?=.*b(?:,|$)) # assert it matches b followed by a comma or end-of-str
(?=.*c(?:,|$)) # assert it matches c followed by a comma or end-of-str
(?:(?:a|b|c)(?:,|$))* # match a, b or c followed by a comma or end-of-str
$ # match end of string
如果您找到 a .*
,则保留断言,但更改正则表达式的最后一部分以使其匹配任何内容。第二个例子:
^ # match start of string
(?=.*a(?:,|$)) # assert it matches a followed by a comma or end-of-str
(?=.*b(?:,|$)) # assert it matches b followed by a comma or end-of-str
(?:[^,]*(,|$))* # match anything followed by a comma or end-of-str
$ # match end of string
当然,您仍然需要解析字符串以生成正则表达式,在这一点上,坦率地说,我更愿意只使用常规代码(它可能也会更快),例如(伪代码):
setRequired = Set(csvRequired.Split(','))
setActual = Set(input.Split(','))
if (setActual.Equals(setRequired)))
{
// passed
}
如果您找到星号,只需将其删除setRequired
并使用.Contains
而不是Equals