string a = "12-345-6789";        Regex reg = new Regex(@"^\d{2}\-\d{3}\-\d{4}$");
        Match m = reg.Match(a);
        if (m.Success)
        {
            MessageBox.Show("OK!");
        }
        else
                       MessageBox.Show("fail!");

解决方案 »

  1.   

    你是不清楚正则 还是不清楚判断是否匹配的方法?
    不清楚正则:
    12-345-6789这样的格式是  ^\d{3}-\d{2}-\d{4}$判断是否匹配用
    string str="用户输入的字符串";
    if(Regex.IsMatch(str,正则表达式)
    {
      匹配
    }
      

  2.   


    string yourStr = ..........;
    if(Regex.IsMatch(yourStr, @"^\d{3}-\d{2}-\d{4}$"))
    {
         MessageBox.Show("符合");
    }
    else
    {
        MessageBox.Show("不符合");
    }
      

  3.   

    如果不写代码,也可以直接拖个验证控件RegularExpressionValidator到页面
    然后在控件的ValidationExpression属性里写上\d{2}-\d{3}-\d{4}   
    就可以验证12-123-1234这种格式的字符串了
      

  4.   

    当然要客户点按钮前验证还要把验证控件和客户填数据的那控件挂勾,设验证控件的ControlToValidate属性为要验证输入数据的控件的ID
      

  5.   

    if(Regex.IsMatch("123-44-3434", @"^\d{3}-\d{2}-\d{4}$"))
    {
         MessageBox.Show("符合");
    }
    else
    {
        MessageBox.Show("不符合");
    }