Created
December 18, 2018 19:22
-
-
Save patelpreet422/656a02aca3ac76a420ea459dceb1921f to your computer and use it in GitHub Desktop.
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> | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
regex re("lo", regex_constants::icase); | |
string target("Hello hello world"); | |
// Match first occurance of pattern | |
/* | |
// smatch. cmatch, wsmatch, wcmatch can be used | |
// these template instance are used to store the actual match result | |
smatch m; | |
regex_search(target, m, re); | |
cout << m.str() << '\n'; | |
*/ | |
/* | |
// Find all the matches | |
// We must iterator to iterate through all the matches | |
// Like smatch, sregex_iterator, cregex_iterator and so on can be used depending on the type of target | |
for(auto match_itr = sregex_iterator(target.begin(), target.end(), re); match_itr != sregex_iterator(); ++match_itr) | |
{ | |
smatch match = *match_itr; | |
cout << match.str() << '\n'; | |
// smatch supports str, position, length member methods as well | |
} | |
*/ | |
/* | |
// Replace all the matched strings | |
string new_str = regex_replace(target, re, "LO"); | |
cout << new_str << '\n'; | |
*/ | |
// Find total how many matches are found | |
auto mbegin = sregex_iterator(target.begin(), target.end(), re); | |
auto mend = sregex_iterator(); | |
auto total_matches = distance(mbegin, mend); | |
cout << total_matches << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment