如何使用正则表达式提取文本中的所有名称
how to extract all the names in a text using regular expression
英文名字就像哈利波特的格式,一个是名一个是姓。但是如何使用c++提取这种模式呢?
一个非常简单的正则表达式是/b([A-Z][a-z]+) ([A-Z][a-z]+)b/
。
编辑:这不能处理奇怪的大写和多余的撇号。
编辑:删除^
和$
,放置单词边界
你可以这样开始。
#include<regex>
#include<iostream>
int main()
{
// regular expression
const std::regex pattern("([A-Z][a-z]+)s([A-Z][a-z]+)");
// the source text
std::string text = "string containing names ...";
const std::sregex_token_iterator end;
for (std::sregex_token_iterator i(text.cbegin(), text.cend(), pattern);
i != end;
++i)
{
std::cout << *i << std::endl;
}
return 0;
}
学习正则表达式有帮助
相关文章:
- 2D数组来自文本输入,中间有空格
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 如何打开并写入一个名称取自C++中字符串的文件
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 有没有办法使用 c++ 实时阅读文本?
- 有没有将变量名称转换为双指针的简短方法?
- 在 Stream C++ 文本之前有一个额外的换行符
- 有没有办法列出所有共享内存对象的名称?
- 将所有 *.txt 文件合并到一个文本文件中,其名称由用户使用 C++ 提供
- 如何创建名称作为文本文件第一行的结构?
- 为什么 GCC 无法优化,除非返回值有名称?
- 非 OOP 版本的"财产"是否有名称?
- C++:此模式是否有名称,是否可以改进
- 从二进制文件中抓取文本时,为什么 xdg_vtnr=8 是我的结果
- 从Windows应用程序的文本框中抓取文本
- 此方法是否已经有名称
- 从没有空格的文本文件中读取数字
- 是否有任何基于文本的进度条实现
- 哪些右值有名称