Java implementation of the regular tool class

  • 2020-04-01 04:14:16
  • OfStack

This article illustrates a Java implementation of the regular utility class. Share with you for your reference. The details are as follows:

Here the implementation of the regular tool class is applicable to: regular phone number, mailbox, QQ number, QQ password, mobile phone number

The Java code is as follows:


package com.zhanggeng.contact.tools;

public class RegexTool {
  
  //If a phone number is passed in, a regular match is made to the phone number
  public static boolean regexPhoneNumber(String phoneNum){
    //The phone number matches
    boolean isPhoneNum_matcher = phoneNum.matches("1[358]\d{9}");
    //If isPhoneNum_matcher is true, return true, else return false
    if(isPhoneNum_matcher)
      return true;
    return false;
  }
  
  //If the mailbox address is passed in, the mailbox is regularly matched
  public static boolean regexEmailAddress(String email){
    //Mailbox matching result
    boolean isEmail_matcher = email.matches("[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\.[a-zA-Z]{2,}){1,3}");
    //If isEmail_matcher value is true, return true, else return false
    if(isEmail_matcher)
      return true;
    return false;
  }
  
  public static boolean regexEmailAddressAndPhoneNum(String phoneNum , String email){
    //The phone number matches
    boolean isPhoneNum_matcher = phoneNum.matches("1[358]\d{9}");
    //Mailbox matching result
    boolean isEmail_matcher = email.matches("[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\.[a-zA-Z]{2,}){1,3}");
    //Matcher value is true, then return true, else return false
    if(isEmail_matcher && isPhoneNum_matcher){
      return true;
    }
    return false;
  }
  
  public static boolean regexQQNumber(String qqNum){
    //QQ number matching results
    boolean isQQNum_matcher = qqNum.matches("[1-9]\d{2,11}");
    if(isQQNum_matcher)
      return true;
    return false;
  }
  
  public static boolean regexPassWord(String pwd){
    //Password match result
    boolean isPassWord_matcher = pwd.matches("[0-9a-zA-Z_@$@]{6,12}");
    if(isPassWord_matcher)
      return true;
    return false;
  }
}

I hope this article has been helpful to your Java programming.


Related articles: