Java 정규 표현식 – 휴대폰 번호 및 전화 번호 확인

한 친구가 필요해서 이 두 개를 썼어요. 말은 안 하고 코드를 봐요.
 /**
   *      httpSession
   * @author :shijing
   * 2016 12 5   3:46:02
   * @return
   */
  public static HttpSession getSession() {
    return getRequest().getSession();
  }
  
  /**
   *      
   * @author :shijing
   * 2016 12 5   4:34:46
   * @param  str
   * @return       true
   */
  public static boolean isMobile(final String str) {
      Pattern p = null;
      Matcher m = null;
      boolean b = false;
      p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); //      
      m = p.matcher(str);
      b = m.matches();
      return b;
  }
  /**
   *       
   * @author :shijing
   * 2016 12 5   4:34:21
   * @param  str
   * @return       true
   */
  public static boolean isPhone(final String str) {
      Pattern p1 = null, p2 = null;
      Matcher m = null;
      boolean b = false;
      p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  //       
      p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         //        
      if (str.length() > 9) {
         m = p1.matcher(str);
         b = m.matches();
      } else {
          m = p2.matcher(str);
         b = m.matches();
      }
      return b;
  }
  
  public static void main(String[] args) {
    String phone = "13900442200";
    String phone2 = "021-88889999";
    String phone3 = "88889999";
    String phone4 = "1111111111";
    //  1
    if(isPhone(phone) || isMobile(phone)){
      System.out.println("1     ");
    }
    //  2
    if(isPhone(phone2) || isMobile(phone2)){
      System.out.println("2     ");
    }
    //  3
    if(isPhone(phone3) || isMobile(phone3)){
      System.out.println("3     ");
    }
    //  4
    if(isPhone(phone4) || isMobile(phone4)){
      System.out.println("4     ");
    }else{
      System.out.println("   ");
    }
  }

좋은 웹페이지 즐겨찾기