Created
April 3, 2025 23:07
-
-
Save jweinst1/7a3fa5f07ca5c74a3b61e6660e259f92 to your computer and use it in GitHub Desktop.
match multiple regex groups in C++
This file contains hidden or 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 <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