1. Regular expressions
Public Static BOOLCheckip (stringStrIP) { //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 BOOLCheckNumber (stringStrText) {String regex="^[1-9]\\d*$";//match numbers and do not start with 0//String regex = "^[0-9]*$"; //Match Numbers if(System.Text.RegularExpressions.Regex.IsMatch (StrText, Regex)) {return true; } Else { return false; } } Public Static BOOLChecknumout (stringstrnum) { //String regex = @ "^[\d,]+$";String regex =@"^[\d][\,\d]*$";//match number, comma 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 address is not legal", "hint",MessageBoxButtons.OK, messageboxicon.information); else MessageBox.Show (mes + "illegal", "hint",MessageBoxButtons.OK, messageboxicon.information);}}
/// <summary> ///verifies whether a string matches numbers, letters, kanji/// </summary> /// <param name= "Isnumber" >match numbers or match numbers, letters, kanji</param> /// <param name= "str" >the input string</param> /// <param name= "Isreturn" >whether you need to return a string that ignores special characters</param> /// <returns></returns> Private BOOLCheckformat (BOOLIsnumber,ref stringStrBOOLIsreturn) { BOOLresult =false; stringRegex ="^[0-9]*$";//Match Numbers if(! Isnumber) regex =@"^[\w]+$";//match numbers, letters, kanji varReg =NewSystem.Text.RegularExpressions.Regex (Regex);// //var str = this. Text.replace (", " ""); varSB =NewStringBuilder (); if(Reg. IsMatch (str)) {result=true; } Else { if(Isreturn) { for(inti =0; I < Str. Length; i++) { if(Reg. IsMatch (Str[i]. ToString ())) {sb. Append (Str[i]. ToString ()); }} str=sb. ToString (); } } returnresult; }
Regular expressions determine IP numbers