zoukankan      html  css  js  c++  java
  • 正则表达式 判断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;
            }
        
  • 相关阅读:
    城市的划入划出效果
    文本溢出省略解决笔记css
    长串英文数字强制折行解决办法css
    Poj 2352 Star
    树状数组(Binary Indexed Trees,二分索引树)
    二叉树的层次遍历
    Uva 107 The Cat in the Hat
    Uva 10336 Rank the Languages
    Uva 536 Tree Recovery
    Uva10701 Pre, in and post
  • 原文地址:https://www.cnblogs.com/xiaochun126/p/4165201.html
Copyright © 2011-2022 走看看