【发布时间】:2013-10-15 11:57:25
【问题描述】:
我有四个单词,在一行中,用 \n 分隔。例如:"aa\ne'sboob\ng-coo\nood\nff"(注意,单词可能不仅包含英文字母,而且不包含'\n'!)
我想在单词级别进行部分匹配:例如部分匹配"oo" 给了我"boob", "coo", and "ood"。
我从模式开始:"^(.*?oo.*?)$",它给了我:"aa\ne'sboob", "g-coo", and "ood"。显然"aa\ne'sboob" 是错误的。
我正在使用 Boost 正则表达式:
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main()
{
std::vector<std::string> v;
std::string text = "aa\ne'sboob\ng-coo\nood\nff";
const char* pattern = "^(.*?oo.*?)$";
boost::regex reg(pattern);
boost::sregex_iterator it(text.begin(), text.end(), reg);
boost::sregex_iterator end;
std::string tmp;
for (; it != end; ++it) {
tmp = it->str();
v.push_back(it->str());
std::cout << tmp << std::endl;
}
std::cout << "total find: " << v.size() << std::endl;
return 0;
}
可以帮我解决这个问题吗?
编辑: 我有一个模式工作,但我不明白。还请帮忙解释。 注意:也许我在正确使用 Boost 正则表达式方面需要帮助。
编辑: 澄清单词可能不仅包含英文字母。还要按照@just-somebody 的建议更新源代码。
非常感谢
【问题讨论】:
-
你说:我有四个单词,在一行中,用
/n分隔。例如:"aa\nboob\ncoo\nood\nff"。字符串中没有/n,它包含4.5行。 -
错字,对不起!感谢您指出。