下面的例子展示了如何在C++11中,利用regex_search()统计一篇英文文章中的单词数:

#include <iostream>
#include <regex>
#include <string>
#include <fstream>
using namespace std;
// 统计单词数
int countword(string& str)
{ try
{
int n = ;
smatch m; // 保存匹配结果的match_result
// 匹配单词的正则表达式,
// 包含了大小写英文字母,连字符,括号,引号
// 这个正则表达式还不完善,可以继续改进
regex ex("\\b[a-zA-Z-()\"]*\\b");
// 用regex_search()在文章中进行匹配,查找单词
while(regex_search(str,m,ex))
{
++n; // 找到,总数加1
cout<<m[]<<endl; // 输出找到的单词
str = m.suffix().str(); // 移动到下一个位置
}
return n;
}
catch (regex_error e)
{
cout<<e.what()<<endl;
}
}
// 读取文件内容
bool readfile(const string name,string& str)
{
ifstream in(name);
if(in.is_open())
{
char line[] = "";
while(true)
{
in.getline(line,);
str += line;
if(in.good())
{
str += '\n';
}
else
{
break;
}
}
return true;
}
else
{
return false;
}
}
int main(int argc,char* argv[])
{
if( != argc)
{
cout<<"argument error. eg. count.exe demo.txt"<<endl;
}
string str("");
if(readfile(argv[],str))
{
int n = countword(str);
cout<<"there are "<<n<<" words in "<<argv[]<<endl;
}
return ;
}

C++语言,统计一篇英文文章中的单词数(用正则表达式实现)的更多相关文章

  1. C语言 统计一篇英文短文中单词的个数

    //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> #define N 1000 void main(){ ] ...

  2. 统计英文文章中各单词的频率,打印频率最高的十个单词(C语言实现)

     一.程序思路及相关代码 首先打开文件,代码如下 FILE *fp; char fname[10]; printf("请输入要分析的文件名:\n"); scanf("%s ...

  3. C#统计英文文本中的单词数并排序

    思路如下:1.使用的Hashtable(高效)集合,记录每个单词出现的次数2.采用ArrayList对Hashtable中的Keys按字母序排列3.排序使用插入排序(稳定) public void S ...

  4. [Swift]LeetCode434. 字符串中的单词数 | Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  5. C#LeetCode刷题之#434-字符串中的单词数​​​​​​​(Number of Segments in a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3941 访问. 统计字符串中的单词个数,这里的单词指的是连续的不是 ...

  6. 力扣(LeetCode)字符串中的单词数 个人题解

    统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" ...

  7. 翻译一篇英文文章,主要是给自己看的——在ASP.NET Core Web Api中如何刷新token

    原文地址 :https://www.blinkingcaret.com/2018/05/30/refresh-tokens-in-asp-net-core-web-api/ 先申明,本人英语太菜,每次 ...

  8. go语言之进阶篇runtime包中 Gosched Goexit GOMAXPROCS的使用

    一.runtime包 1.Gosched的使用 runtime.Gosched() 用于让出CPU时间片,让出当前goroutine的执行权限,调度器安排其他等待的任务运行,并在下次某个时候从该位置恢 ...

  9. 软件工程导论课后习题Github作业(把一个英文句子中的单词次序逆序,单词中字母正常排列)

    Java源代码    package yly; import java.util.Scanner; public class ruanjian { public static void main(St ...

随机推荐

  1. Some Tips About VS2015

    Xaml edit bug about vs2013+ sometimes, vs will say some class not found in the namespace. but, we co ...

  2. 50.ISE布局布线错误

    ERROR:Pack:1654 - The timing-driven placement phase encountered an error. 原因:时钟输出引脚直接接在I/O上了: 方法:在时钟 ...

  3. Labview实现频率调制(FM)

    Labview实现频率调制(FM) 频率调制的原理: 自己的实现为三角函数分解 根据这个公式在Labview中连线则可以得到最终的波形输出 实现效果 从频域图中可以看出,载波信号的频率被调制,原本为双 ...

  4. 【转载】如何在FPGA设计环境中添加加时序约束

    转自:http://bbs.ednchina.com/BLOG_ARTICLE_198929.HTM 如何在FPGA设计环境中加时序约束    在给FPGA做逻辑综合和布局布线时,需要在工具中设定时序 ...

  5. QT 字符串相等间距字符间增加字符

    /*************************************************Function: formatString()Description: 将十六进制字串每字节中间加 ...

  6. iOS 进阶 第十六天(0419)

    0419 任何view默认不支持多点触控,有一个属性设置Multiple Touch,设置为Yes即可支持多点触控 触摸移动一个view,让view也跟着动代码 关于触摸的一些解释: 注意:touch ...

  7. sql总结

    sql总结 sql总结 where字句中使用的运算符 定义外键 定义主键 多表联合查询 统计函数 数据类型 sql语句格式 转换函数 null函数 运算符 日期 求某天是星期几 日期天数差 next_ ...

  8. color the python console text

    //install termcolor module cd \ cd python27 cd scripts pip install termcolor pip install colorama // ...

  9. git创建分支并提交项目

    git 创建分支, 切换分支, 合并分支, 删除分支及提交[commit提交到本地仓库push名利提交到远程服务器], 检出[pull], 冲突修改, 本地仓库同步远程服务器[pul和push命令l] ...

  10. Android -- 系统信息(内存、cpu、sd卡、电量、版本)获取

    内存(ram)                                                                              android的总内存大小信息 ...