is string an Email by regex - Java java.util.regex

Java examples for java.util.regex:Match Email

Description

is string an Email by regex

Demo Code

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class Main{
    public static void main(String[] argv){
        String input = "java2s.com";
        System.out.println(isEmail(input));
    }/*w w  w . j av  a2s.com*/
    
    public static boolean isEmail(String input) {
        if (isEmpty(input))
            return false;
        Pattern pattern = Pattern
                .compile("^[A-Za-z0-9._%+-]+@(\\w+\\.)+[a-zA-Z]{2,3}");
        return pattern.matcher(input).matches();
    }
    
    public static boolean isEmpty(String str) {
        return (str == null || str.trim().length() == 0);
    }
    
    public static String trim(String str) {
        return (str == null) ? null : str.trim();
    }
}

Related Tutorials