Skip to content

Instantly share code, notes, and snippets.

@sergey-pashaev
Last active December 14, 2016 00:03
Show Gist options
  • Select an option

  • Save sergey-pashaev/9502609 to your computer and use it in GitHub Desktop.

Select an option

Save sergey-pashaev/9502609 to your computer and use it in GitHub Desktop.
c++ regex simple test template
all:
g++ -o regex-test regex.cpp -Wall -std=c++11 -lboost_regex
#include <string>
#include <vector>
#include <regex>
#include <boost/regex.hpp>
using std::cout;
using std::endl;
using std::string;
using std::vector;
// rx - regular expression
// ... - list of strings to match
#define TEST(rx, ...) { \
string s = rx; \
vector<string> tests = { __VA_ARGS__ }; \
regex_test(s, tests); \
}
void regex_test(string &rx_str, vector<string> &tests) {
cout << "rx : " << rx_str << endl;
boost::regex rx(rx_str, std::regex_constants::basic);
boost::smatch res;
for (size_t i = 0; i < tests.size(); i++) {
if (regex_match(tests[i], res, rx)) {
cout << "ok - " << tests[i];
for (size_t i = 1; i < res.size(); i++) {
cout << " " << i << ":" << res[i];
}
cout << endl;
} else {
cout << "bad - " << tests[i] << endl;
}
}
cout << endl;
}
int main() {
TEST("/api/files/(\\d+)/(\\d+)",
"/api/files/0/1",
"/api/files/012/3",
"/api/files/0/123",
);
TEST("/api/files/(\\d+)/(\\d+)/content",
"/api/files/0/1/content",
"/api/files/012/3/content",
"/api/files/0/123/content",
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment