Skip to content

Instantly share code, notes, and snippets.

@cutlassfish
Last active September 18, 2016 06:56
Show Gist options
  • Save cutlassfish/f59a4c7b96bccc18f2f02feeb14c4f3d to your computer and use it in GitHub Desktop.
Save cutlassfish/f59a4c7b96bccc18f2f02feeb14c4f3d to your computer and use it in GitHub Desktop.
#include <regex>
#include <algorithm>
#include <iostream>
int main()
{
std::string str("version 1.20.300.400");
std::regex pattern("([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)");
{
std::sregex_token_iterator begin(str.begin(), str.end(), pattern, {1,3});
std::sregex_token_iterator end;
std::for_each(begin, end, [](const std::string& sub) {
std::cout << sub << std::endl; // 1 and 300
});
}
{
std::sregex_token_iterator begin(str.begin(), str.end(), pattern, {2,4});
std::sregex_token_iterator end;
std::for_each(begin, end, [](const std::string& sub) {
std::cout << sub << std::endl; // 20 and 400
});
}
{
std::sregex_token_iterator begin(str.begin(), str.end(), pattern, 0);
std::sregex_token_iterator end;
std::for_each(begin, end, [](const std::string& sub) {
std::cout << sub << std::endl; // 1.20.300.400
});
}
{
std::sregex_token_iterator begin(str.begin(), str.end(), pattern, -1);
std::sregex_token_iterator end;
std::for_each(begin, end, [](const std::string& sub) {
std::cout << sub << std::endl; // version
});
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment