Last active
September 18, 2016 06:56
-
-
Save cutlassfish/f59a4c7b96bccc18f2f02feeb14c4f3d to your computer and use it in GitHub Desktop.
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 <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