#include <iostream>
#include <regex>
#include <string>
int main() {
std::string text1 = "Hello, world!";
std::regex pattern1(R"(Hello, world!)");
if (std::regex_match(text1, pattern1)) {
std::cout << "[Match] The entire string matches the pattern." << std::endl;
} else {
std::cout << "[Match] The entire string does NOT match." << std::endl;
}
std::string text2 = "The quick brown fox jumps over the lazy dog.";
std::regex pattern2(R"(brown|red)");
if (std::regex_search(text2, pattern2)) {
std::cout << "[Search] Found a match (brown or red) in the text." << std::endl;
} else {
std::cout << "[Search] No match found." << std::endl;
}
std::string text3 = "Emails: alice@example.com, bob@work.org, charlie@home.net";
std::regex email_pattern(R"((\w+)@(\w+\.\w+))");
std::cout << "\n[Iterate] All email addresses found:" << std::endl;
std::smatch match;
std::string::const_iterator search_start(text3.cbegin());
while (std::regex_search(search_start, text3.cend(), match, email_pattern)) {
std::cout << " Full: " << match[0]
<< " | User: " << match[1]
<< " | Domain: " << match[2] << std::endl;
search_start = match.suffix().first;
}
std::string text4 = "Today is 2026-06-30, tomorrow will be 2026-07-01.";
std::regex date_pattern(R"((\d{4})-(\d{2})-(\d{2}))");
std::string replacement = "$3/$2/$1";
std::string result = std::regex_replace(text4, date_pattern, replacement);
std::cout << "\n[Replace] Original: " << text4 << std::endl;
std::cout << " Result: " << result << std::endl;
std::string text5 = "C++ is case sensitive, but REGEX can be case-insensitive.";
std::regex case_sensitive_pattern(R"(c\+\+)");
std::regex case_insensitive_pattern(R"(c\+\+)", std::regex::icase);
bool found_sensitive = std::regex_search(text5, case_sensitive_pattern);
bool found_insensitive = std::regex_search(text5, case_insensitive_pattern);
std::cout << "\n[Case‑insensitive] Case-sensitive search found? "
<< (found_sensitive ? "Yes" : "No") << std::endl;
std::cout << " Case‑insensitive search found? "
<< (found_insensitive ? "Yes" : "No") << std::endl;
std::string input = "12345";
std::regex integer_pattern(R"(^[+-]?\d+$)");
if (std::regex_match(input, integer_pattern)) {
std::cout << "\n[Validation] \"" << input << "\" is a valid integer." << std::endl;
} else {
std::cout << "\n[Validation] \"" << input << "\" is NOT a valid integer." << std::endl;
}
return 0;
}