Java常见的正则表达式运用

Java常见的正则表达式运用
1 package com.fsti.icop.util.regexp; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public final class RegExpValidatorUtils { 7 /** 8 * 验证邮箱 9 * 10 * @param 待验证的字符串 11 * @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b> 12 */ 13 public static boolean isEmail(String str) { 14 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})(\\]?)$"; 15 return match(regex, str); 16 } 17 18 /** 19 * 验证IP地址 20 * 21 * @param 待验证的字符串 22 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 23 */ 24 public static boolean isIP(String str) { 25 String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)"; 26 String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$"; 27 return match(regex, str); 28 } 29 30 /** 31 * 验证网址Url 32 * 33 * @param 待验证的字符串 34 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 35 */ 36 public static boolean IsUrl(String str) { 37 String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"; 38 return match(regex, str); 39 } 40 41 /** 42 * 验证电话号码 43 * 44 * @param 待验证的字符串 45 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 46 */ 47 public static boolean IsTelephone(String str) { 48 String regex = "^(\\d{3,4}-)?\\d{6,8}$"; 49 return match(regex, str); 50 } 51 52 /** 53 * 验证输入密码条件(字符与数据同时出现) 54 * 55 * @param 待验证的字符串 56 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 57 */ 58 public static boolean IsPassword(String str) { 59 String regex = "[A-Za-z]+[0-9]"; 60 return match(regex, str); 61 } 62 63 /** 64 * 验证输入密码长度 (6-18位) 65 * 66 * @param 待验证的字符串 67 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 68 */ 69 public static boolean IsPasswLength(String str) { 70 String regex = "^\\d{6,18}$"; 71 return match(regex, str); 72 } 73 74 /** 75 * 验证输入邮政编号 76 * 77 * @param 待验证的字符串 78 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 79 */ 80 public static boolean IsPostalcode(String str) { 81 String regex = "^\\d{6}$"; 82 return match(regex, str); 83 } 84 85 /** 86 * 验证输入手机号码 87 * 88 * @param 待验证的字符串 89 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 90 */ 91 public static boolean IsHandset(String str) { 92 String regex = "^[1]+[3,5]+\\d{9}$"; 93 return match(regex, str); 94 } 95 96 /** 97 * 验证输入身份证号 98 * 99 * @param 待验证的字符串 100 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 101 */ 102 public static boolean IsIDcard(String str) { 103 String regex = "(^\\d{18}$)|(^\\d{15}$)"; 104 return match(regex, str); 105 } 106 107 /** 108 * 验证输入两位小数 109 * 110 * @param 待验证的字符串 111 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 112 */ 113 public static boolean IsDecimal(String str) { 114 String regex = "^[0-9]+(.[0-9]{2})?$"; 115 return match(regex, str); 116 } 117 118 /** 119 * 验证输入一年的12个月 120 * 121 * @param 待验证的字符串 122 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 123 */ 124 public static boolean IsMonth(String str) { 125 String regex = "^(0?[[1-9]|1[0-2])$"; 126 return match(regex, str); 127 } 128 129 /** 130 * 验证输入一个月的31天 131 * 132 * @param 待验证的字符串 133 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 134 */ 135 public static boolean IsDay(String str) { 136 String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$"; 137 return match(regex, str); 138 } 139 140 /** 141 * 验证日期时间 142 * 143 * @param 待验证的字符串 144 * @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 145 */ 146 public static boolean isDate(String str) { 147 // 严格验证时间格式的(匹配[2002-01-31], [1997-04-30], 148 // [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01]) 149 // String regex = 150 // "^((((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)))))$"; 151 // 没加时间验证的YYYY-MM-DD 152 // String regex = 153 // "^((((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-))$"; 154 // 加了时间验证的YYYY-MM-DD 00:00:00 155 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$"; 156 return match(regex, str); 157 } 158 159 /** 160 * 验证数字输入 161 * 162 * @param 待验证的字符串 163 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 164 */ 165 public static boolean IsNumber(String str) { 166 String regex = "^[0-9]*$"; 167 return match(regex, str); 168 } 169 170 /** 171 * 验证非零的正整数 172 * 173 * @param 待验证的字符串 174 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 175 */ 176 public static boolean IsIntNumber(String str) { 177 String regex = "^\\+?[1-9][0-9]*$"; 178 return match(regex, str); 179 } 180 181 /** 182 * 验证大写字母 183 * 184 * @param 待验证的字符串 185 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 186 */ 187 public static boolean IsUpChar(String str) { 188 String regex = "^[A-Z]+$"; 189 return match(regex, str); 190 } 191 192 /** 193 * 验证小写字母 194 * 195 * @param 待验证的字符串 196 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 197 */ 198 public static boolean IsLowChar(String str) { 199 String regex = "^[a-z]+$"; 200 return match(regex, str); 201 } 202 203 /** 204 * 验证验证输入字母 205 * 206 * @param 待验证的字符串 207 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 208 */ 209 public static boolean IsLetter(String str) { 210 String regex = "^[A-Za-z]+$"; 211 return match(regex, str); 212 } 213 214 /** 215 * 验证验证输入汉字 216 * 217 * @param 待验证的字符串 218 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 219 */ 220 public static boolean IsChinese(String str) { 221 String regex = "^[\u4e00-\u9fa5],{0,}$"; 222 return match(regex, str); 223 } 224 225 /** 226 * 验证验证输入字符串 227 * 228 * @param 待验证的字符串 229 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 230 */ 231 public static boolean IsLength(String str) { 232 String regex = "^.{8,}$"; 233 return match(regex, str); 234 } 235 236 /** 237 * @param regex 238 * 正则表达式字符串 239 * @param str 240 * 要匹配的字符串 241 * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false; 242 */ 243 private static boolean match(String regex, String str) { 244 Pattern pattern = Pattern.compile(regex); 245 Matcher matcher = pattern.matcher(str); 246 return matcher.matches(); 247 } 248 249 // 3. 检查字符串重复出现的词 250 // 251 // private void btnWord_Click(object sender, EventArgs e) 252 // { 253 // System.Text.RegularExpressions.MatchCollection matches = 254 // System.Text.RegularExpressions.Regex.Matches(label1.Text, 255 // 256 // @"\b(?<word>\w+)\s+(\k<word>)\b", 257 // System.Text.RegularExpressions.RegexOptions.Compiled | 258 // System.Text.RegularExpressions.RegexOptions.IgnoreCase); 259 // if (matches.Count != 0) 260 // { 261 // foreach (System.Text.RegularExpressions.Match match in matches) 262 // { 263 // string word = match.Groups["word"].Value; 264 // MessageBox.Show(word.ToString(),"英文单词"); 265 // } 266 // } 267 // else { MessageBox.Show("没有重复的单词"); } 268 // 269 // 270 // } 271 // 272 // 4. 替换字符串 273 // 274 // private void button1_Click(object sender, EventArgs e) 275 // { 276 // 277 // string strResult = 278 // System.Text.RegularExpressions.Regex.Replace(textBox1.Text, 279 // @"[A-Za-z]\*?", textBox2.Text); 280 // MessageBox.Show("替换前字符:" + "\n" + textBox1.Text + "\n" + "替换的字符:" + "\n" 281 // + textBox2.Text + "\n" + 282 // 283 // "替换后的字符:" + "\n" + strResult,"替换"); 284 // 285 // } 286 // 287 // 5. 拆分字符串 288 // 289 // private void button1_Click(object sender, EventArgs e) 290 // { 291 // //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁 292 // foreach (string s in 293 // System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*")) 294 // { 295 // textBox2.Text+=s; //依次输出 "甲乙丙丁" 296 // } 297 // 298 // } 299 300 }

-----------------------------------------------------摘文于

https://www.cnblogs.com/silentjesse/p/3242701.html,转载请注意标明文章出处,尊重原作者的成果。

猜你喜欢

转载自www.cnblogs.com/liuzeyu12a/p/9976872.html