Match m = Regex.Match(textBox1.Text, @"^(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(\.?\d)\.)){4}$");
           MessageBox.Show(m.Success.ToString());

解决方案 »

  1.   

    先在上面加上命名空间
    System.Text.RegularExpressions;            Regex r = new Regex(@"^(\d+)\.(\d+)\.(\d+)\.(\d+)$");
                if (!r.IsMatch(this.textBox1.Text.Trim()))
                {
                    MessageBox.Show("你输入的IP不合法!");
                }
      

  2.   

    try...Regex reg = new Regex(@"^(?:(?:[1-9]?[0-9]|1[0-9]{2}|2(?:[0-4][0-9]|5[0-5]))\.){3}(?:[1-9]?[0-9]|1[0-9]{2}|2(?:[0-4][0-9]|5[0-5]))$");
    if (reg.IsMatch(yourStr))
    {
        MessageBox.Show("合法!");
    }
    else
    {
        MessageBox.Show("不合法!");
    }