Skip to content

Instantly share code, notes, and snippets.

@IceSandwich
Created February 7, 2025 03:37
Show Gist options
  • Save IceSandwich/7c04caf9b0c8c114c28420fbbf62f696 to your computer and use it in GitHub Desktop.
Save IceSandwich/7c04caf9b0c8c114c28420fbbf62f696 to your computer and use it in GitHub Desktop.
std::regex
#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