如何使用增强正则表达式在单行中匹配单词
how to match word in single line using Boost Regex?
我有四个单词,在一行中,用n
分隔。 例如:"aane'sboobng-coonoodnff"
(注意,单词可能不仅包含英文字母,但不包含""!
我想在单词级别进行部分匹配:例如部分匹配"oo"
给了我"boob", "coo", and "ood"
。
我从模式开始:"^(.*?oo.*?)$"
,它给了我:"aane'sboob", "g-coo", and "ood"
。显然"aane'sboob"
错了。
我正在使用Boost Regex:
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main()
{
std::vector<std::string> v;
std::string text = "aane'sboobng-coonoodnff";
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有人的建议更新源。
非常感谢
当你想要[a-z]*
时,不要在你的正则表达式中使用.*
我有这个模式对我来说很好用:
"^([^\n.]*?oo.*?)$"
但我期待更优雅的解决方案。
谢谢。
bw*oow*b
应该会有所帮助。 Perl 正则表达式语法。
编辑,因为 OP 争论答案...
我对发布的代码进行了以下更改:
- 添加了
#include <boost/regex.hpp>
- 将函数更改为
int main(void)
- 将模式更改为
const char* pattern = "\b\w*oo\w*\b";
编译、运行并得到:
boob
coo
ood
total find: 3
相关文章:
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 如何根据单词在文本中出现的概率输出单词
- 当输入在带有空格的单行中给出时,cin 如何在 while 循环中工作?
- 如何在C++中的单行中返回常量数组中的值
- 是否可以在单行中获取第一种类型的参数包?
- 解锈化代码格式化程序删除C++单行中的空格
- 打印以 "a" 开头的单词(字符串中)
- 通过在每个位置从特定位置的单词池中选择单词,有效地生成所有序列
- 在单词词典中提取以片段开头/包含/结束的单词
- 通过单行中的XOR交换整数.在c++11中真的允许这样做吗
- 比较数组C++中单词列表中的单词
- 如何在单行中初始化结构并将其添加到矢量
- 如何使用增强正则表达式在单行中匹配单词
- 这是从连续的单词序列中提取任意范围的位的最有效方法
- 在C/C++中,在没有数组的单行中快速输入字符串
- 检查字符串是否在单词列表中的最有效方法
- 如何将数字"Bind"到一串单词/短语中,以便我可以循环调用它?
- 试图从c++文本文件的单行中提取字符串的一部分
- 从单词列表中构造回文