1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
void testMatch(const boost::regex &ex, const string st) {
cout << "Matching " << st << endl;
if (boost::regex_match(st, ex)) {
cout << " matches" << endl;
}
else {
cout << " doesn’t match" << endl;
}
}
void testSearch(const boost::regex &ex, const string st) {
cout << "Searching " << st << endl;
string::const_iterator start, end;
start = st.begin();
end = st.end();
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
while(boost::regex_search(start, end, what, ex, flags))
{
cout << " " << what.str() << endl;
start = what[0].second;
}
}
int main(int argc, char *argv[])
{
static const boost::regex ex("[Rr]eg...r");
testMatch(ex, "regular");
testMatch(ex, "abc");
testMatch(ex, "some regular expressions are Regxyzr");
testMatch(ex, "RegULarexpressionstring");
testSearch(ex, "regular");
testSearch(ex, "abc");
testSearch(ex, "some regular expressions are Regxyzr");
testSearch(ex, "RegULarexpressionstring");
return 0;
}
|