How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in C++

1 Answer

0 votes
#include <iostream>
#include <regex>
#include <vector>

int main() {
    // Declare the regex pattern
    std::regex pattern("htt+p");

    // Test strings
    std::vector<std::string> testStrings = {"http", "htttp", "httttp", "httpp", "htp"};

    // Check matches
    for (const auto& test : testStrings) {
        bool match = std::regex_match(test, pattern);
        std::cout << "Matches \"" << test << "\": " << std::boolalpha << match << std::endl;
    }
}

// Matches "httpp": True or false, depending on how matches() method works


/*
run:

Matches "http": true
Matches "htttp": true
Matches "httttp": true
Matches "httpp": false
Matches "htp": false

*/

 



answered May 15, 2025 by avibootz
edited May 15, 2025 by avibootz
...