Generation

regexSun, 26 Jan 2025

Email checker

Here's a simple email checker in C++ that checks if a given email is valid or not. This program uses a regular expression to validate the email. ```cpp #include <iostream> #include <regex> #include <string> // Function to check if an email is valid bool isValidEmail(const std::string& email) { // Regular expression for a valid email std::regex pattern("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"); return std::regex_match(email, pattern); } int main() { // Test emails std::string emails[] = {"test@example.com", "invalid_email", "test@example.co.uk", "test@example.com.au"}; // Check each email for (const auto& email : emails) { if (isValidEmail(email)) { std::cout << email << " is a valid email." << std::endl; } else { std::cout << email << " is not a valid email." << std::endl; } } return 0; } ``` This program uses the `std::regex` class to define a regular expression that matches a valid email. The `std::regex_match` function is then used to check if the given email matches this pattern. Here's a breakdown of the regular expression used: - `^` matches the start of the string. - `[a-zA-Z0-9._%+-]+` matches one or more alphanumeric characters, dots, underscores, percent signs, plus signs, or hyphens. - `@` matches the at symbol. - `[a-zA-Z0-9.-]+` matches one or more alphanumeric characters, dots, or hyphens. - `\\.` matches a period (escaped with a backslash because `.` has a special meaning in regular expressions). - `[a-zA-Z]{2,}` matches the domain extension (it must be at least 2 characters long). - `$` matches the end of the string. This regular expression is not foolproof and may not match all valid email addresses, but it should cover most common cases.

Want to kickstart your project?Use the new AI Studio to create your code