如何将序列与长度可变的可选尾部匹配。
对于示例requiredOptional
,我想具有一个与required
,requiredO
,requiredOp
,requiredOpt
,requiredOpti
匹配的表达式。 .. requiredOptional
。
答案 0 :(得分:-1)
怎么样
Regex GetRegex(int length) {
return new Regex(String.Format("requiredO{0}p{1}t{2}i{3}o{4}n{5}a{6}l{7}", length > 7 ? "?" : "", length > 6 ? "?" : "", length > 5 ? "?" : "", length > 4 ? "?" : "", length > 3 ? "?" : "", length > 2 ? "?" : "", length > 1 ? "?" : "", length > 0 ? "?" : ""), RegexOptions.Compiled);
}