Check email address with regex for gmail - Java Regular Expressions

Java examples for Regular Expressions:Email

Description

Check email address with regex for gmail

Demo Code

     /*from   w w  w .  j a  v a 2s.  co m*/

import java.util.*;
import java.util.regex.*;
import java.util.Collections;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        String emailRegEx = ".+@gmail\\.com$";
        Pattern pattern = Pattern.compile(emailRegEx);
        List<String> myList = new ArrayList<String>();
        for(int a0 = 0; a0 < N; a0++){
            String firstName = in.next();
            String emailID = in.next();
            Matcher matcher = pattern.matcher(emailID);
            if (matcher.find()){
                myList.add(firstName);
            }
        }
        Collections.sort(myList);
        for (String name : myList){
            System.out.println(name);
        }
        in.close();
    }
}

Related Tutorials