Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个字符串,只有当它有 2 个字符并且用逗号分隔时才有效。
AD,AC,AN,JP (valid) AD (valid) if user enter only one it must be validate
如果是数字或逗号以外的任何其他字符或长度大于 2,则必须返回无效。
AD,12,AN,JP (invalid) AAD,12,AN,JP (invalid) AA,CC,ANA,JP (invalid) AA,#C,AA,JP (invalid)
这假设输入总是大写:
var reggie = new Regex(@"^[A-Z]{2}(,[A-Z]{2})*$");
如果除了验证输入之外,您还想提取数据,则可以对逗号执行简单的拆分(不需要正则表达式拆分):
if (reggie.IsMatch(inputString)) string[] values = string.Split(',');