仍然工作如果您想一個正則表達式,這裏的my take on this:
^ # 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
如果你找到一個.*
,你保持斷言,但改變正則表達式的最後部分,使其能夠匹配任何東西。 Second example:
^ # 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
我寧願爲此編寫正常的代碼...我知道這是完全可以強制正則表達式的解決方案,但我寧願不去那裏。 – nhahtdh
'〜'是什麼。我無法理解你想做什麼...... – Anirudha
@ Fake.It.Til.U.Make.It:可能是排列組合。 – nhahtdh