Email Validator using Regex in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <regex.h>
int main() {
    char e[100]; scanf("%s", e);
    regex_t r;
    regcomp(&r, "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$", REG_EXTENDED);
    printf(!regexec(&r, e, 0, NULL, 0) ? "Valid" : "Invalid");
}

C Output

Input:
[email protected]

Output:
Valid



C++ Program

#include <iostream>
#include <regex>
using namespace std;
int main() {
    string e; cin >> e;
    cout << (regex_match(e, regex("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")) ? "Valid" : "Invalid");
}

C++ Output

Input:
my.email@host

Output:
Invalid



JAVA Program

import java.util.*;
public class Main {
    public static void main(String[] a) {
        String e = new Scanner(System.in).next();
        System.out.println(e.matches("^[\\w._%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$") ? "Valid" : "Invalid");
    }
}

JAVA Output

Input:
[email protected]

Output:
Valid



Python Program

import re
e = input()
print("Valid" if re.match(r'^[\w\.-]+@[\w\.-]+\.\w{2,}$', e) else "Invalid")

Python Output

Input:
test@com

Output:
Invalid



In-Depth Explanation
Example
Suppose the input is [email protected]. The regular expression verifies the following:

Starts with alphabets, numbers, dots, or underscores

Followed by @

Then contains a domain name with dots (such as domain.com or abc.co.in)

Ends with 2 or more alphabetic characters as TLD (such as .com, .in)

This pattern is accepted and labeled as Valid. However, something like test@com is Invalid due to the absence of an appropriate domain format.

Real-Life Analogy
When you sign up on a site, it immediately tests if the email you typed "looks valid." It is not attempting to validate the inbox — only if the format is correct. If you typed hello@, it immediately informs you it's incorrect. That validation is what this code emulates.

Why It Matters
Email validation is crucial in:

Signup forms

Login pages

Newsletter services

Authentication flows

Anti-spam and fake entries

Without email validation, a system can be inundated with rubbish data, bots, or unreachables.

Learning Insights
This exercise teaches:

Basics of regex (regular expressions)

Syntax for character classes, quantifiers, anchors

Language-specific how-to use regex (such as regex_match, re.match, matches())

It also enhances understanding of:

Input format validation

Pattern matching

How email format rules are encoded in regex

Understanding regex is like becoming good at search patterns — a fundamental skill in text analysis, automation, parsing, and data science.

Interview & Project Usage
regex questions in interviews check your:

Pattern matching

Syntax familiarity

Logic to match structured input

In actual projects, email validation is ubiquitous — web apps, mobile apps, APIs, and internal tools. Practically every user-facing system requires it.

Email validation with regex is a real-world and useful programming challenge that imparts essential regex concepts, format validation, and live input validation. Whether you are developing a login page, server-side API, or mobile application, this reasoning guarantees smooth and clean data input. With the most concise and clean solutions in C, C++, Java, and Python, this code makes you sure to develop secure, user-friendly systems working on any platform.