可以的情况如下所示
S1234
M1234
S1234-S2345   M1234-M2345
S1234/S2345/S3456   M1234/M2345/M3456
S1234(S2345)  M1234(M2345)其中S和M不能同时出现在一个字符串中。

解决方案 »

  1.   

    >>>其中S和M不能同时出现在一个字符串中 string[] slist = {"S1234",
    "M1234",
    "S1234-S2345",
    "M1234-M2345",
    "S1234/S2345/S3456",
    "M1234/M2345/M3456",
    "S1234(S2345)",
    "M1234(M2345)",
    "M1234-M2345/M3456",
    "M1234-M2345(M3456)"}; Regex re = new Regex(@"^([SM])\d{4}([-/]\1\d{4}|[(]\1\d{4}[)])*$"); foreach (string s in slist)
    Console.WriteLine("{0} matches?{1}", s, re.IsMatch(s)); //if the last two are not allowed, try re = new Regex(@"^([SM])\d{4}((-\1\d{4}|)*|(/\1\d{4}|)*|([(]\1\d{4}[)])*)$"); foreach (string s in slist)
    Console.WriteLine("{0} matches?{1}", s, re.IsMatch(s));