Validate Email (Regex)

Validate email format using a simple regex.

JavaIntermediate
Java
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

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