Last active
June 3, 2018 22:14
-
-
Save MannyGozzi/f40148d81bd21b63c4fbc81ec33bac0c 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 <iostream> | |
#include <string> | |
#include <regex> | |
int main() | |
{ | |
//string that we want to find matches in | |
std::string string = "The dog went to the dog park. " | |
"There were numerous other doggies"; | |
//the object we want to hold matches in | |
//called smatch because it holds matches that were found in a string | |
//hence the 's' followed by match | |
std::smatch match; | |
//the pattern we want to find within the string | |
//the question mark means that (gies) is optional | |
//so this means we want to "match dog" or "doggies" | |
std::regex pattern("dog(gies)?"); | |
//sregex iterator ( 's' is for string ) | |
//sregex iterator returns a bool that indicated if it found a match | |
//the iterator has a member function called str() that returns the most recent match | |
//the iterator itself is a pointer so we have to access using iterator->str() or (*iterator).str() | |
//the "end" variable defaults to a end of string iterator | |
//notice that we create 2 sregex_iterators within the for loop's initialization portion | |
//repeat until no more matches are found | |
for (std::sregex_iterator iterator(string.cbegin(), string.cend(), pattern), end; | |
iterator != end; ++iterator) | |
{ | |
//print the match we just found | |
std::cout << iterator->str() << std::endl; | |
} | |
std::cin.get(); //wait until user presses any key so that the program ends | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment