首页 > 代码库 > 正则表达式 判断IP 数字

正则表达式 判断IP 数字

1、正则表达式

 

            public static bool checkIP(string strIP)            {                //string regex = @"^(2[0-4]\d | 25[0-5] | [01]?\d?[1-9])\." +                //                @"(2[0-4]\d | 25[0-5] | [01]?\d?\d)\." +                //                @"(2[0-4]\d | 25[0-5] | [01]?\d?\d)\." +                //                @"(2[0-4]\d | 25[0-5] | [01]?\d?\d)$";                String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."                                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";                if (System.Text.RegularExpressions.Regex.IsMatch(strIP, regex))                {                    return true;                }                else                {                    return false;                }            }            public static bool checkNumber(string strText)            {                String regex = "^[1-9]\\d*$"; //匹配数字并且不以0开头                // String regex = "^[0-9]*$";  //匹配数字                 if (System.Text.RegularExpressions.Regex.IsMatch(strText, regex))                {                    return true;                }                else                {                    return false;                }            }            public static bool checkNumOut(string strNum)            {                //String regex = @"^[\d,]+$" ;                 String regex = @"^[\d][\,\d]*$"; //匹配数字,逗号                if (System.Text.RegularExpressions.Regex.IsMatch(strNum, regex))                {                    return true;                }                else                {                    return false;                }            }

            public static void checkIP(TextBox txt, string mes)            {                if (txt.Text.Trim() != "" && !Global.Methods.checkIP(txt.Text.Trim()))                {                    txt.Text = "";                    txt.Focus();                    if (mes == "")                        MessageBox.Show("IP地址不合法", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                    else                        MessageBox.Show(mes + "不合法", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                }            }
 

 

  /// <summary>        /// 验证字符串是否 匹配数字、字母、汉字        /// </summary>        /// <param name="isNumber">匹配数字还是匹配数字、字母、汉字</param>        /// <param name="str">输入的字符串</param>        /// <param name="IsReturn">是否需要返回忽略特殊字符的字符串</param>        /// <returns></returns>        private bool checkFormat(bool IsNumber, ref string str, bool IsReturn)        {            bool result = false;            string regex = "^[0-9]*$";  //匹配数字             if (!IsNumber) regex = @"^[\w ]+$"; //匹配数字、字母、汉字            var reg = new System.Text.RegularExpressions.Regex(regex);//             //var str = this.Text.Replace(" ", "");            var sb = new StringBuilder();            if (reg.IsMatch(str))            {                result = true;            }            else            {                if (IsReturn)                {                    for (int i = 0; i < str.Length; i++)                    {                        if (reg.IsMatch(str[i].ToString()))                        {                            sb.Append(str[i].ToString());                        }                    }                    str = sb.ToString();                }            }            return result;        }    

 

正则表达式 判断IP 数字