c++使⽤正则表达式提取关键字的⽅法
下⾯看下c++通过正则表达式提取关键字,代码如下所⽰:
string text = "岳云鹏的对象叫铁锤";
regex pattern("(.*)的对象叫(.*)");
smatch results;
if (regex_match(text, results, pattern)) {
for (auto it = results.begin(); it != d(); ++it)
cout << *it << endl;
}
else {
cout << "match failed: " << text << endl;
}
// 岳云鹏的对象叫铁锤
// 岳云鹏
// 铁锤
下⾯看下C++正则表达式提取匹配到的字符串
/*
* 输⼊是789.123.456, 输出的是789
*/
void get()
{
std::regex ip_reg("(.*)\.123\.456");
std::smatch matchResult;
string inputStr;
std::getline(std::cin,inputStr);
//正则匹配
if (std::regex_match(inputStr,matchResult,ip_reg))
regex匹配{
cout << "Match: ";
//打印⼦表达式结果
for (size_t i = 1; i < matchResult.size(); ++i)
{
cout << matchResult[i] << " ";
}
}
else
{
cout << "Not Match!";
}
}
总结
以上所述是⼩编给⼤家介绍的c++使⽤正则表达式提取关键字的⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论