Home > Java > javaTutorial > body text

Java regular expression verification email and phone number examples

高洛峰
Release: 2017-01-07 10:02:03
Original
1386 people have browsed it

下面的代码使用正则表达式验证输入格式包括了验证邮箱和验证手机号码

package com.firewolf.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 使用正则表达式验证输入格式
 * @author liuxing
 *
 */
public class RegexValidateUtil {
 public static void main(String[] args) {
  System.out.println(checkEmail("14_8@qw.df"));
  System.out.println(checkMobileNumber("071-3534452"));
 }
 /**
  * 验证邮箱
  * @param email
  * @return
  */
 public static boolean checkEmail(String email){
  boolean flag = false;
  try{
    String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    Pattern regex = Pattern.compile(check);
    Matcher matcher = regex.matcher(email);
    flag = matcher.matches();
   }catch(Exception e){
    flag = false;
   }
  return flag;
 }
 
 /**
  * 验证手机号码
  * @param mobiles
  * @return
  */
 public static boolean checkMobileNumber(String mobileNumber){
  boolean flag = false;
  try{
    Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
    Matcher matcher = regex.matcher(mobileNumber);
    flag = matcher.matches();
   }catch(Exception e){
    flag = false;
   }
  return flag;
 }
}
Copy after login


更多java正则表达式验证邮箱、电话号码示例相关文章请关注PHP中文网!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template