一个有用的正则表达式判定工具类

前端之家收集整理的这篇文章主要介绍了一个有用的正则表达式判定工具类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package com.zhanggeng.contact.tools;
  2.  
  3. /**
  4. * RegexTool is used to regex the string,such as : phone,qq,password,email .
  5. *
  6. * @author ZHANGGeng
  7. * @version v1.0.1
  8. * @since JDK5.0
  9. *
  10. */
  11.  
  12. public class RegexTool {
  13. /**
  14. *
  15. * @param phoneNum 传入的参数仅仅是一个电话号码时,调用方法
  16. * @return 如果匹配正确,return true,else return else
  17. */
  18. //如果传进来的是电话号码,则对电话号码进行正则匹配
  19. public static boolean regexPhoneNumber(String phoneNum){
  20. //电话号码匹配结果
  21. boolean isPhoneNum_matcher = phoneNum.matches("1[358]\\d{9}");
  22. //如果isPhoneNum_matcher is true,则return true,else return false
  23. if(isPhoneNum_matcher)
  24. return true;
  25. return false;
  26. }
  27. /**
  28. *
  29. * @param email 传入的参数仅仅是一个邮箱地址时,调用方法
  30. * @return 如果匹配正确,return true,else return false
  31. */
  32. //如果传进来的是邮箱地址,则对邮箱进行正则匹配
  33. public static boolean regexEmailAddress(String email){
  34. //邮箱匹配结果
  35. boolean isEmail_matcher = email.matches("[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,}){1,3}");
  36. //如果isEmail_matcher value is true,则 return true,else return false
  37. if(isEmail_matcher)
  38. return true;
  39. return false;
  40. }
  41. /**
  42. *
  43. * @param phoneNum 传入的电话号码
  44. * @param email 传入的邮箱地址
  45. * @return 如果匹配正确,return true,else return false
  46. */
  47. public static boolean regexEmailAddressAndPhoneNum(String phoneNum,String email){
  48. //电话号码匹配结果
  49. boolean isPhoneNum_matcher = phoneNum.matches("1[358]\\d{9}");
  50. //邮箱匹配结果
  51. boolean isEmail_matcher = email.matches("[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3}");
  52. //matcher value is true,else return false
  53. if(isEmail_matcher && isPhoneNum_matcher){
  54. return true;
  55. }
  56. return false;
  57. }
  58. /**
  59. *
  60. * @param qqNum 传入的QQ
  61. * @return 如果匹配正确,return true, else return false
  62. */
  63. public static boolean regexQQNumber(String qqNum){
  64. //QQ号匹配结果
  65. boolean isQQNum_matcher = qqNum.matches("[1-9]\\d{2,11}");
  66. if(isQQNum_matcher)
  67. return true;
  68. return false;
  69. }
  70. /**
  71. *
  72. * @param pwd 传入的是 密码
  73. * @return 如果匹配正确,满足密码规则,return true, else return false
  74. */
  75. public static boolean regexPassWord(String pwd){
  76. //密码匹配结果
  77. boolean isPassWord_matcher = pwd.matches("[0-9a-zA-Z_@$@]{6,12}");
  78. if(isPassWord_matcher)
  79. return true;
  80. return false;
  81. }
  82. }

猜你在找的正则表达式相关文章