Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created April 3, 2025 23:07
Show Gist options
  • Save jweinst1/7a3fa5f07ca5c74a3b61e6660e259f92 to your computer and use it in GitHub Desktop.
Save jweinst1/7a3fa5f07ca5c74a3b61e6660e259f92 to your computer and use it in GitHub Desktop.
match multiple regex groups in C++
#include <regex>
int main(int argc, char const *argv[])
{
const std::string foo = "hello \n sir \n";
std::vector<EventPos> pairs;
generateEvents(foo, '\n', pairs);
std::printf("%zu\n", pairs.size());
const std::string sample = "remote= bar foo=4 six=bar fhhfghfgdhgdf do=555";
const std::regex sampleRex("([a-zA-Z0-9_]+)\\s*=\\s*([a-zA-Z0-9_]+)");
auto wordsBegin = std::sregex_iterator(sample.begin(), sample.end(), sampleRex);
auto wordsEnd = std::sregex_iterator();
for (std::sregex_iterator i = wordsBegin; i != wordsEnd; ++i) {
std::printf("match pos %zu size %zu\n", i->position(), i->size());
for (const auto& m : *i) {
std::printf("%s\n", m.str().c_str());
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment