Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created July 11, 2019 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/482695a3eb9a1f901ca690ce10596261 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/482695a3eb9a1f901ca690ce10596261 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<string>
#include<vector>
#include<regex>
using namespace std;
int main() {
string str;
while (true) {
cin >> str;
smatch m; //typedef std::match_results<string>
//We can define two groups here to extract the user name and
//domain name
regex e("([[:w:]]+)@([[:w:]]+)\.com");
//search the string for regular expression e and save the
//results to m
bool match = regex_search(str, m, e);
cout << "match size: " << m.size() << endl;
for(int i = 0; i < m.size(); ++i){
//print out the matched results
cout << "m[" << i <<"]: str() = " << m[i].str() <<endl;
}
//prefix is everything before the first matched character
cout <<"m.prefix().str(): " << m.prefix().str() << endl;
//suffic is everything after the last matched character
cout <<"m.suffix().str(): " << m.suffix().str() <<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment