Boost_regex

1
2
#include < boost/regex.hpp>
using namespace boost;
  1. regex_match

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*)");
    std::string in="select name from table";
    cmatch what;
    if(regex_match(in.c_str(), what, expression))
    {

    for(int i=0;i< what.size();i++)
    {
    cout<< what[i].first<< "|"<<what[i].second<< endl;
    cout<< "str :"<< what[i].str()<< endl;
    }
    }
  2. regex_search

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    boost::regex r("(a+)");
    string content = "bbbaaaaacccaaaaddddaaaeeeaaa";
    boost::smatch m;
    string::const_iterator strstart = content.begin();
    string::const_iterator strend = content.end();
    while(boost::regex_search(strstart, strend, m, r))
    {
    //0 for the whole, m[i] is the i-th group
    cout << m[0] << endl;
    //m[i].first and m[i].second stands for the begin idx and end idx
    strstart = m[0].second;
    }

    Note regex_match(in.c_str(), what, expression) is for complete match while regex_match(in.c_str(), what, expression) is for incomplete match.
    regex_match(in.c_str(), expression, target_exp).

  3. special setting for regex

    1
    boost::regex e2(my_expression, boost::regex::icase); \\case insensitive