java 正则表达式

  1. {

  2. 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})(\\]?)$”;

  3. return match(regex, str);

  4. }

  5. /**

  6. * 验证IP地址

  7. * @param 待验证的字符串

  8. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  9. */

  10. public static boolean isIP(String str)

  11. {

  12. String num = “(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)”;

  13. String regex = “^” + num + “\\.” + num + “\\.” + num + “\\.” + num + “$”;

  14. return match(regex, str);

  15. }

  16. /**

  17. * 验证网址Url

  18. * @param 待验证的字符串

  19. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  20. */

  21. public static boolean IsUrl(String str)

  22. {

  23. String regex = “http(s)?😕/([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?”;

  24. return match(regex, str);

  25. }

  26. /**

  27. * 验证电话号码

  28. * @param 待验证的字符串

  29. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  30. */

  31. public static boolean IsTelephone(String str)

  32. {

  33. String regex = “^(\\d{3,4}-)?\\d{6,8}$”;

  34. return match(regex, str);

  35. }

  36. /**

  37. * 验证输入密码条件(字符与数据同时出现)

  38. * @param 待验证的字符串

  39. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  40. */

  41. public static boolean IsPassword(String str)

  42. {

  43. String regex = “[A-Za-z]+[0-9]”;

  44. return match(regex, str);

  45. }

  46. /**

  47. * 验证输入密码长度 (6-18位)

  48. * @param 待验证的字符串

  49. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  50. */

  51. public static boolean IsPasswLength(String str)

  52. {

  53. String regex = “^\\d{6,18}$”;

  54. return match(regex, str);

  55. }

  56. /**

  57. * 验证输入邮政编号

  58. * @param 待验证的字符串

  59. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  60. */

  61. public static boolean IsPostalcode(String str)

  62. {

  63. String regex = “^\\d{6}$”;

  64. return match(regex, str);

  65. }

  66. /**

  67. * 验证输入手机号码

  68. * @param 待验证的字符串

  69. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  70. */

  71. public static boolean IsHandset(String str)

  72. {

  73. String regex = “^[1]+[3,5]+\\d{9}$”;

  74. return match(regex, str);

  75. }

  76. /**

  77. * 验证输入身份证号

  78. * @param 待验证的字符串

  79. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  80. */

  81. public static boolean IsIDcard(String str)

  82. {

  83. String regex = “(^\\d{18}KaTeX parse error: Got function '\\' with no arguments as superscript at position 5: )|(^\̲\̲\\d{15})”;

  84. return match(regex, str);

  85. }

  86. /**

  87. * 验证输入两位小数

  88. * @param 待验证的字符串

  89. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  90. */

  91. public static boolean IsDecimal(String str)

  92. {

  93. String regex = “^[0-9]+(.[0-9]{2})?$”;

  94. return match(regex, str);

  95. }

  96. /**

  97. * 验证输入一年的12个月

  98. * @param 待验证的字符串

  99. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  100. */

  101. public static boolean IsMonth(String str)

  102. {

  103. String regex = “^(0?[[1-9]|1[0-2])$”;

  104. return match(regex, str);

  105. }

  106. /**

  107. * 验证输入一个月的31天

  108. * @param 待验证的字符串

  109. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  110. */

  111. public static boolean IsDay(String str)

  112. {

  113. String regex = “^((0?[1-9])|((1|2)[0-9])|30|31)$”;

  114. return match(regex, str);

  115. }

  116. /**

  117. * 验证日期时间

  118. * @param 待验证的字符串

  119. * @return 如果是符合网址格式的字符串,返回 true ,否则为 false 

  120. */

  121. public static boolean isDate(String str)

  122. {

  123. //严格验证时间格式的(匹配[2002-01-31], [1997-04-30], [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])

  124. //        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)))))$”;

  125. //没加时间验证的YYYY-MM-DD

  126. //        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-))$”;

  127. //加了时间验证的YYYY-MM-DD 00:00:00

  128. 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$”;

  129. return match(regex, str);

  130. }

  131. /**

  132. * 验证数字输入

  133. * @param 待验证的字符串

  134. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  135. */

  136. public static boolean IsNumber(String str)

  137. {

  138. String regex = “^[0-9]*$”;

  139. return match(regex, str);

  140. }

  141. /**

  142. * 验证非零的正整数

  143. * @param 待验证的字符串

  144. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  145. */

  146. public static boolean IsIntNumber(String str)

  147. {

  148. String regex = “^\\+?[1-9][0-9]*$”;

  149. return match(regex, str);

  150. }

  151. /**

  152. * 验证大写字母

  153. * @param 待验证的字符串

  154. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  155. */

  156. public static boolean IsUpChar(String str)

  157. {

  158. String regex = “^[A-Z]+$”;

  159. return match(regex, str);

  160. }

  161. /**

  162. * 验证小写字母

  163. * @param 待验证的字符串

  164. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  165. */

  166. public static boolean IsLowChar(String str)

  167. {

  168. String regex = “^[a-z]+$”;

  169. return match(regex, str);

  170. }

  171. /**

  172. * 验证验证输入字母

  173. * @param 待验证的字符串

  174. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  175. */

  176. public static boolean IsLetter(String str)

  177. {

  178. String regex = “^[A-Za-z]+$”;

  179. return match(regex, str);

  180. }

  181. /**

  182. * 验证验证输入汉字

  183. * @param 待验证的字符串

  184. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  185. */

  186. public static boolean IsChinese(String str)

  187. {

  188. String regex = “^[\u4e00-\u9fa5],{0,}$”;

  189. return match(regex, str);

  190. }

  191. /**

  192. * 验证验证输入字符串

  193. * @param 待验证的字符串

  194. * @return 如果是符合格式的字符串,返回 true ,否则为 false 

  195. */

  196. public static boolean IsLength(String str)

  197. {

  198. String regex = “^.{8,}$”;

  199. return match(regex, str);

  200. }

  201. /**

  202. * @param regex 正则表达式字符串

  203. * @param str 要匹配的字符串

  204. * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;

  205. */

  206. private static boolean match(String regex, String str)

  207. {

  208. Pattern pattern = Pattern.compile(regex);

  209. Matcher matcher = pattern.matcher(str);

  210. return matcher.matches();

  211. }

  212. //    3. 检查字符串重复出现的词

  213. //

  214. //    private void btnWord_Click(object sender, EventArgs e)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值