Valid Email Pattern Check in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <regex.h>

int main() {
    char email[100];
    printf("Enter email: ");
    scanf("%s", email);

    regex_t regex;
    int result;

    // Simple regex for email validation
    result = regcomp(&regex, "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$", REG_EXTENDED);
    if (result) {
        printf("Could not compile regex\n");
        return 1;
    }

    result = regexec(&regex, email, 0, NULL, 0);
    if (!result)
        printf("Valid Email\n");
    else
        printf("Invalid Email\n");

    regfree(&regex);
    return 0;
}

C Output

Input:  
[email protected] 

Output:  
Valid Email


C++ Program

#include <iostream>
#include <regex>
using namespace std;

int main() {
    string email;
    cout << "Enter email: ";
    cin >> email;

    regex pattern("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");

    if (regex_match(email, pattern))
        cout << "Valid Email" << endl;
    else
        cout << "Invalid Email" << endl;

    return 0;
}

C++ Output

Input:  
[email protected] 

Output:  
Valid Email


JAVA Program

import java.util.Scanner;
import java.util.regex.*;

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

        String regex = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(email);

        if (matcher.matches())
            System.out.println("Valid Email");
        else
            System.out.println("Invalid Email");

        sc.close();
    }
}

JAVA Output

Input:  
[email protected]  

Output:  
Valid Email


Python Program

import re

email = input("Enter email: ")

pattern = r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'

if re.match(pattern, email):
    print("Valid Email")
else:
    print("Invalid Email")

Python Output

Input:  
wrong-email@com

Output:  
Invalid Email


Explanation
Example
If you enter "[email protected]", the program verifies if it is in the correct email format:

A username (student123) consisting of alphabets, numbers, or special characters such as ., _, %, +, -.

An @ symbol between the user and domain.

A correct domain name (college.edu) that contains a top-level domain such as .com, .edu, .org, .net, etc.

If all are consistent, it is Valid Email; else, it's Invalid Email.

Real-Life Analogy
Consider an email address as a proper address. Just as a letter has to have a house number, street, city, and postal code in a standard form to be delivered to the location, so too must an email. If the address is malformed, the postman can't deliver it. Likewise, email servers reject malformed email IDs. 

Why It Matters
Verifying if an email address is valid is one of the most typical everyday programming challenges. Sites, applications, and registration systems all need to ask users for a valid email address. If validation is lacking, the system could gather fake or invalid information, resulting in security threats, spam, or breakdowns in communication. 

Learning Insights
This course educates us on how regex operates in various programming languages. Regex is a versatile tool that enables pattern matching within strings. Mastering the construction and testing of regex is essential for input validation, data parsing, and tackling everyday text-based issues. For new learners, this course is an introduction to regex, while for experienced learners, it's an entry to solid validation methodologies.

Interview and Real-World Relevance
During interviews, candidates are usually questioned on regex fundamentals, email validation, and string pattern verification. Hiring managers want to assess whether you know how to enforce data verification. In actual projects, each signup form, login system, or API needs to validate user input in order to avoid errors or malicious attacks. This program demonstrates both your knowledge of regex as well as your capability to put practical solutions into practice.

SEO-Optimized Closing
Verifying an email pattern is a basic programming task that is used extensively in web programming, database management, and user authentication systems. New programmers studying C, C++, Java, and Python can use email verification programs to reinforce their learning of string manipulation and regex. With this easy-to-understand description and working source code in several programming languages, students who are planning to appear for coding interviews, online tests, or actual projects can learn quickly how to implement a program to verify valid email patterns.