test_cpp-regexp-boost.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <boost/regex.hpp>
#include <boost/bind.hpp>
namespace {
void parse_type_string(const std::string& pattern,
const std::string& str,
std::vector<std::string>& tokens) {
boost::regex re;
try {
re = pattern;
} catch (boost::regex_error& e) {
std::cerr << "invalid regexp:" << e.what() << std::endl;
return;
}
boost::cmatch matches;
if (boost::regex_match(str.c_str(), matches, re)) {
for (size_t i = 1; i < matches.size() ; ++i) {
const std::string token(matches[i].first,
matches[i].second);
tokens.push_back(token);
}
} else {
std::cout << "no match" << std::endl;
return;
}
}
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "usage:"
<< argv[0] << " REGEXP STRING"
<< std::endl;
return 1;
}
const std::string p(argv[1]);
const std::string s(argv[2]);
std::vector <std::string> tok;
parse_type_string(p, s, tok);
std::copy(tok.begin(),
tok.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
}