Validate Email (Regex)

Validate email format using a simple regex.

IntermediateTopic: Module 4: String Programs
Back

Java Validate Email (Regex) Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter email: ");
        String email = sc.nextLine();

        String regex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
        boolean valid = Pattern.matches(regex, email);
        System.out.println(valid ? "Valid" : "Invalid");
        sc.close();
    }
}
Output
Enter email: [email protected]
Valid

Understanding Validate Email (Regex)

We use a basic regex to ensure local-part@domain structure.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents