请协助正确的RegEx匹配。任何2或6个字母后跟任意数字组合。
These would be valid:
AA123456
ABCDEF654321
DD3212343123213
AS0000012
These would not:
DDD12345
AAAA123
AAAA12345 FVCSA
答案 0 :(得分:3)
如果你想要模式或模式,为什么不将两个(子)模式与or:|
结合起来,例如(我假设字母应该是 capital < / em>在所提供的所有示例中都有A..Z
,并且应该至少有一个数字)。
^(([A-Z]{2})|([A-Z]{6}))[0-9]+$
实施
string[] tests = new string[] {
"AA123456",
"ABCDEF654321",
"DD3212343123213",
"AS0000012",
"DDD12345",
"AAAA123",
"AAAA12345 FVCSA",
};
string testReport = string.Join(Environment.NewLine, tests
.Select(item => Regex.IsMatch(item, "^(([A-Z]{2})|([A-Z]{6}))[0-9]+$")
? $"valid: {item}"
: $"invalid: {item}" ));
Console.WriteLine(testReport);
结果:
valid: AA123456
valid: ABCDEF654321
valid: DD3212343123213
valid: AS0000012
invalid: DDD12345
invalid: AAAA123
invalid: AAAA12345 FVCSA
答案 1 :(得分:0)
如果至少有一个号码:
^([A-Za-z]{2}|[A-Za-z]{6})[0-9]+$
如果没有最小数字位数:
^([A-Za-z]{2}|[A-Za-z]{6})[0-9]*$