Show regex1.cc syntax highlighted
#include <string>
#include <boost/regex.hpp>
#include <iostream>
#include <cstdlib>
using namespace boost;
void dump_matches(smatch &m)
{
int i;
for(i=0; i<m.size(); i++) {
std::cout << "\tmatch[" << i << "] = " << m[i].str() << std::endl;
}
}
int main()
{
int i;
smatch matches;
regex pattern("^[abc],\\s+\\((today|tomorrow)\\)\\s+[0-9]+.*([/])$", boost::regbase::perl|boost::regbase::icase);
boost::regex command("^(?:(\\S+)\\s*|\"[^\"]+\"\\s*)+$", boost::regbase::perl);
std::string s = "B, (TODAY) 102 this is tone/";
std::string s2 = "B, (ToMoRRow) 102 hello word";
if(regex_match(s, matches, pattern)) {
dump_matches(matches);
std::cout << "conversion: " << std::atoi(matches[2].str().c_str()) << std::endl;
} else
std::cout << "No match on s" << std::endl;
if(regex_match(s2, matches, pattern)) {
dump_matches(matches);
std::cout << "conversion: " << std::atoi(matches[2].str().c_str()) << std::endl;
} else
std::cout << "No match on s2" << std::endl;
std::string s3 = "B \"a, b, c, d\" 120 41 inbox";
if(regex_match(s3, matches, command)) {
dump_matches(matches);
} else
std::cout << "No match on command string" << std::endl;
return 0;
}
See more files for this project here