Created
February 7, 2025 03:37
-
-
Save IceSandwich/7c04caf9b0c8c114c28420fbbf62f696 to your computer and use it in GitHub Desktop.
std::regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <regex> | |
#include <string> | |
int main() { | |
std::string str{ "The ip address 127.0.0.1:8080 had been blocked." }; | |
std::string pattern{ "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})" }; | |
std::cout << "input str: " << str << std::endl; | |
std::cout << "pattern str: " << pattern << std::endl; | |
std::regex re(pattern, std::regex::icase); | |
if (std::smatch ret; std::regex_search(str, ret, re)) { | |
std::cout << "found the str: " << ret.str() << std::endl; | |
std::cout << "has subgroup: " << ret.size() - 1 << std::endl; | |
for (auto&& [i, subgroup] = std::make_pair(1, ret.begin()+1); subgroup != ret.cend(); ++subgroup, ++i) { | |
const std::ptrdiff_t begin = ret.position(i); | |
std::cout << "\t - [" << i << "] at (" << begin << ", " << begin + subgroup->length() << ") : " << subgroup->str() << std::endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment