Check email Format using regex - Java java.util.regex

Java examples for java.util.regex:Match Email

Introduction

The following code shows how to check email Format

Demo Code

//package com.java2s;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String email = "asdf@java2s.com";
        System.out.println(emailFormat(email));
        /*from  w  w w.j ava  2 s .  c o  m*/
        email = "java2s.com";
        System.out.println(emailFormat(email));
    }

    public static boolean emailFormat(String email) {
        String pattern = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return Pattern.compile(pattern).matcher(email).find();
    }
}

Related Tutorials