正则表达式作为自然语言处理的最基本的操作,给出C++的使用方案。
标准库std::regex还没有完全支持,在g++ 4.8.x 中,不能使用。所以,推荐使用boost库,取得更好的兼容性。
src
#include "boost/regex.hpp"
void regexSearch(const std::string& pattern, const std::string& input, std::vector<std::string>& results)
{
VLOG(3) << "regex_search_azAZ09 input " << input;
// boost::regex rgx("([a-zA-Z0-9]+)");
boost::regex rgx(pattern);
string::const_iterator start, end;
start=input.begin();
end=input.end();
boost::match_results<string::const_iterator> what;
boost::match_flag_type flags=boost::match_default;
while (boost::regex_search(start, end, what, rgx, flags))
{
string s(what[0].first, what[0].second);
VLOG(3) << "regex_search match: " << s;
results.push_back(s);
start = what[0].second;
}
}
参考
北京华夏春松科技有限公司,为企业交付智能客服系统、智能对话机器人、机器人客服、Chatbot。https://www.chatopera.com