- #include <boost/xpressive/xpressive_dynamic.hpp>
- #include <boost/shared_ptr.hpp>
- #include <iostream>
- #include <string>
- const int E_SUCCESS = 1;
- const int E_FAILED = 2;
- class CRegex
- {
- public:
- CRegex(){}
- ~CRegex(){}
- int strMatch(const std::string& strSource);
- private:
- static boost::xpressive::sregex_compiler rc;
- };
- boost::xpressive::sregex_compiler CRegex::rc;
- int CRegex::strMatch(const std::string& strSource)
- {
- std::string strCompile = "^\\\\d{6}(1|2)\\\\d{3}(0|1)\\\\d[0-3]\\\\d\\\\d{3}(x|X|\\\\d)";
- rc[strSource] = rc.compile(strCompile);
- if (boost::xpressive::regex_match(strSource,rc[strSource]))
- return E_SUCCESS;
- else
- return E_FAILED;
- }
- int main()
- {
- boost::shared_ptr<CRegex> pObj(new CRegex);
- //@Test
- std::string strSource = "42900419871005161g";
- if (E_FAILED == pObj->strMatch(strSource))
- {
- std::cout<<"Failed--->"<<strSource.c_str()<<" is not matched!!!"<<std::endl;
- }
- else
- std::cout<<"Success--->"<<strSource.c_str()<<" matched!!!"<<std::endl;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/3107201410092.html
来源: http://www.codesnippet.cn/detail/3107201410092.html