Created
September 13, 2009 08:11
-
-
Save aggieben/186124 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> | |
using namespace std; | |
int main( ) { | |
string s, sre; | |
regex re; | |
cmatch matches; | |
while(true) | |
{ | |
cout << "Expression: "; | |
cin >> sre; | |
if (sre == "quit") | |
{ | |
break; | |
} | |
cout << "String: "; | |
cin >> s; | |
try | |
{ | |
// Assignment and construction initialize the FSM used | |
// for regexp parsing | |
re = sre; | |
} | |
catch (regex_error& e) | |
{ | |
cout << sre << " is not a valid regular expression: \"" | |
<< e.what() << "\"" << endl; | |
continue; | |
} | |
// if (regex_match(s.begin(), s.end(), re)) | |
if (regex_match(s.c_str(), matches, re)) | |
{ | |
// matches[0] contains the original string. matches[n] | |
// contains a sub_match object for each matching | |
// subexpression | |
for (int i = 1; i < matches.size(); i++) | |
{ | |
// sub_match::first and sub_match::second are iterators that | |
// refer to the first and one past the last chars of the | |
// matching subexpression | |
string match(matches[i].first, matches[i].second); | |
cout << "\tmatches[" << i << "] = " << match << endl; | |
} | |
} | |
else | |
{ | |
cout << "The regexp \"" << sre << "\" does not match \"" << s << "\"" << endl; | |
} | |
} | |
} |
Thanks for the tip. I do most of my C++ programming with GCC, which includes some of the tr1
classes in std
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm using Visual C++ 2008 Express Edition and in order to compile this code I have to add the directive:
using namespace tr1;