日期:2014-05-17  浏览次数:20525 次

判断IP是否合法,请教
C# code

 public class CheckIp
    {
        public static bool IsCorrenctIp(string ip)
        {
            string pattrn = @"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])";

            if (System.Text.RegularExpressions.Regex.IsMatch(ip, pattrn))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }


正则表达式应该怎么写

------解决方案--------------------
http://blog.csdn.net/soulspark/article/details/1608272

------解决方案--------------------
private const string IP = @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))$";
/// <summary>
/// 判断IP地址
/// </summary>
/// <returns>返回匹配成功与否</returns>
public static bool JudgeIPAddress(object obj)
{
string str = obj.ToString().Trim();
return Regex.IsMatch(str,IP);
}
------解决方案--------------------
贴上Label那里的代码吧