#include <iostream>
#include <regex>
#include <string>

int main() {
    // ========== 1. Full match (exact match) ==========
    std::string text1 = "Hello, world!";
    std::regex pattern1(R"(Hello, world!)");  // Raw string literal

    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;
    }

    // ========== 2. Partial search (find a substring) ==========
    std::string text2 = "The quick brown fox jumps over the lazy dog.";
    std::regex pattern2(R"(brown|red)");  // Match either "brown" or "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;
    }

    // ========== 3. Iterate over all matches with capturing groups ==========
    std::string text3 = "Emails: alice@example.com, bob@work.org, charlie@home.net";
    // Pattern to capture username and domain separately
    std::regex email_pattern(R"((\w+)@(\w+\.\w+))");

    std::cout << "\n[Iterate] All email addresses found:" << std::endl;
    std::smatch match;  // Match results for strings
    std::string::const_iterator search_start(text3.cbegin());
    while (std::regex_search(search_start, text3.cend(), match, email_pattern)) {
        // Full match is match[0], capture groups are match[1], match[2], ...
        std::cout << "  Full: " << match[0] 
                  << " | User: " << match[1] 
                  << " | Domain: " << match[2] << std::endl;
        // Advance search start to after the current match
        search_start = match.suffix().first;
    }

    // ========== 4. Replace all occurrences ==========
    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}))");
    // Replace YYYY-MM-DD with DD/MM/YYYY
    std::string replacement = "$3/$2/$1";  // Use backreferences in replacement

    std::string result = std::regex_replace(text4, date_pattern, replacement);
    std::cout << "\n[Replace] Original: " << text4 << std::endl;
    std::cout << "          Result: " << result << std::endl;

    // ========== 5. Case‑insensitive matching ==========
    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;

    // ========== 6. Validating input (example: a simple integer) ==========
    std::string input = "12345";
    std::regex integer_pattern(R"(^[+-]?\d+$)");  // Optional sign, digits only

    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;
}

results matching ""

    No results matching ""