我是regex语法的新手,正在寻找一种符合以下条件的方法:
字符串有1个字母字符,其余为数字
字符串以至少1位数字开头,但不超过3位
后跟字符是单个字母字符(大写或小写字母a-Z)
后跟4到6位数字
有效数据示例:
1A1111
1A11111
1A111111
11A1111
11A11111
11A111111
111A1111
111A11111
111A111111
我发现的大多数示例都匹配一个或多个值,因此我在努力匹配特定数量的字符以及在哪里可以找到它们。
例如:
字符串开头匹配1个或多个数字:
@"^\d"
或确保字符串至少包含一个Alpha字符:
bool match = Regex.IsMatch(tokenString, @"(?=.*[^a-zA-Z])", RegexOptions.IgnoreCase);
但这并不能说明只能有1个字母字符。
最佳答案
这会工作
^\d{1,3}[a-zA-Z]\d{4,6}$
分解:
^ - match at beginning
\d{1,3} - one to three digits
[a-zA-Z] - one letter a-z or A-Z
\d{4,6} - followed by between 4 and 6 digits
$ - and that's end of the string...