[boost] regex

posted in: boost, C/C++程式設計 | 0

一直match不成功,原來是我忘了在C++裡面需要二個\\阿…(因為\在C++裡面是脫逸字元,有特殊的意義)

以下記錄一下match日期成功的方式:

regex expression("\\d{4}-\\d{2}-\\d{2}.\\d{2}:\\d{2}:\\d{2}.*"); 
string tmpStr = "2011-02-24 13:31:37";

cmatch what; 
if(regex_match(tmpStr.c_str(), what, expression)){
   cout < < "match" << endl;
   std::string msg(what[1].first, what[1].second);
   std::cout<< msg.c_str() << std::endl;
}

Leave a Reply

Your email address will not be published. Required fields are marked *