package com.fsti.icop.util.regexp; import java.util.regex.Matcher; import java.util.regex.Pattern; public final class RegExpValidatorUtils { /** * Verify email * * @param String to be validated * @return If it is a matching string, return <b>true </b>, otherwise <b>false </b> */ public static boolean isEmail(String str) { String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; return match(regex, str); } /** * Verify IP address * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean isIP(String str) { String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)"; String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$"; return match(regex, str); } /** * Verify URL Url * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsUrl(String str) { String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"; return match(regex, str); } /** * Verify phone number * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsTelephone(String str) { String regex = "^(\\d{3,4}-)?\\d{6,8}$"; return match(regex, str); } /** * Verify input password conditions (characters and data appear at the same time) * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsPassword(String str) { String regex = "[A-Za-z]+[0-9]"; return match(regex, str); } /** * Verify the length of the input password (6-18 digits) * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsPasswLength(String str) { String regex = "^\\d{6,18}$"; return match(regex, str); } /** * Validate the zip code entered * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsPostalcode(String str) { String regex = "^\\d{6}$"; return match(regex, str); } /** * Verify the entered mobile number * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsHandset(String str) { String regex = "^[1]+[3,5]+\\d{9}$"; return match(regex, str); } /** * Verify input ID number * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsIDcard(String str) { String regex = "(^\\d{18}$)|(^\\d{15}$)"; return match(regex, str); } /** * Validate input with two decimal places * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsDecimal(String str) { String regex = "^[0-9]+(.[0-9]{2})?$"; return match(regex, str); } /** * Validate 12 months of the year entered * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsMonth(String str) { String regex = "^(0?[[1-9]|1[0-2])$"; return match(regex, str); } /** * Validate entering 31 days of the month * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsDay(String str) { String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$"; return match(regex, str); } /** * Verify date and time * * @param String to be validated * @return If it is a string that matches the URL format, return <b>true </b>, otherwise <b>false </b> */ public static boolean isDate(String str) { // Strictly verify the time format (match [2002-01-31], [1997-04-30], // [2004-01-01]) does not match ([2002-01-32], [2003-02-29], [04-01-01]) // String regex = // "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$"; // YYYY-MM-DD without time verification // String regex = // "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"; // YYYY-MM-DD 00:00:00 with time validation added String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; return match(regex, str); } /** * Validate numeric input * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsNumber(String str) { String regex = "^[0-9]*$"; return match(regex, str); } /** * Verify non-zero positive integer * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsIntNumber(String str) { String regex = "^\\+?[1-9][0-9]*$"; return match(regex, str); } /** * Verify capital letters * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsUpChar(String str) { String regex = "^[A-Z]+$"; return match(regex, str); } /** * Validate lowercase letters * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsLowChar(String str) { String regex = "^[a-z]+$"; return match(regex, str); } /** * Validation validates the input letters * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsLetter(String str) { String regex = "^[A-Za-z]+$"; return match(regex, str); } /** * Validate the input Chinese characters * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsChinese(String str) { String regex = "^[\u4e00-\u9fa5],{0,}$"; return match(regex, str); } /** * Validate the validation input string * * @param String to be validated * @return If it is a string that conforms to the format, return <b>true </b>, otherwise <b>false </b> */ public static boolean IsLength(String str) { String regex = "^.{8,}$"; return match(regex, str); } /** * @param regex * regular expression string * @param str * string to match * @return If str conforms to the regular expression format of regex, return true, otherwise return false; */ private static boolean match(String regex, String str) { Pattern pattern = Pattern.compile(regex); Matches matches = pattern.matcher (str); return matcher.matches(); } // 3. Check the string for repeated words // // private void btnWord_Click(object sender, EventArgs e) // { // System.Text.RegularExpressions.MatchCollection matches = // System.Text.RegularExpressions.Regex.Matches(label1.Text, // // @"\b(?<word>\w+)\s+(\k<word>)\b", // System.Text.RegularExpressions.RegexOptions.Compiled | // System.Text.RegularExpressions.RegexOptions.IgnoreCase); // if (matches.Count != 0) // { // foreach (System.Text.RegularExpressions.Match match in matches) // { // string word = match.Groups["word"].Value; // MessageBox.Show(word.ToString(),"English word"); // } // } // else { MessageBox.Show("No repeated words"); } // // // } // // 4. Replace the string // // private void button1_Click(object sender, EventArgs e) // { // // string strResult = // System.Text.RegularExpressions.Regex.Replace(textBox1.Text, // @"[A-Za-z]\*?", textBox2.Text); // MessageBox.Show("Previous character: " + "\n" + textBox1.Text + "\n" + "Replaced character: " + "\n" // + textBox2.Text + "\n" + // // "Replaced characters:" + "\n" + strResult,"Replace"); // // } // // 5. Split the string // // private void button1_Click(object sender, EventArgs e) // { // //Example: A 025-8343243 B 0755-2228382 C 029-32983298389289328932893289 D // foreach (string s in // System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*")) // { // textBox2.Text+=s; //Output "A, B, C, D" in turn // } // // } }