Created
August 31, 2012 04:28
-
-
Save mbohun/3549074 to your computer and use it in GitHub Desktop.
test_cpp-regexp-boost.cpp
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 <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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment