下面的例子展示了如何在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. php远程图片抓取存放到本地路径并生成缩略图

    private function _getcontent($content)    {               $img_dir='../Public/Img/Ycimg'; //远程图片抓取存放 ...

  2. html a 链接

    1.链接元素 文本.图像.热区 2.标记 描述性文字 target窗口形式 _self:在自身窗口打开(默认) _blank:在新窗口打开 _parent:在父窗口打开 _top:在顶窗口打开 框架或 ...

  3. Querying mergeinfo requires version 3 of the FSFS filesystem schema

    环境: jdk 1.7; svn  3.0.4;  TortoiseSVN 1.7.13 Subversion 1.7.10; IntelliJ IDEA 13.1.1;win7 64位系统 之前那个 ...

  4. 使用getElementById获取xml中的指定元素

    Document有一个getElementById的方法,在文档中的解释是:     返回具有带给定值的 ID 属性的 Element.如果不存在此类元素,则此方法返回 null.如果多个元素具有带该 ...

  5. Leetcode-Construct Binary Tree from inorder and preorder travesal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  6. 项目结队开发---NABC分析(成员)

    一.简介 项目名称:校园导航 特点:手机app,简便易用,适合对铁大地形不了解.路痴者使用. 二.NABC分析 N(need):对于新生报到,学生家长参观校园等想要了解校园路线者,本app软件将带给你 ...

  7. android开发 wifi开发不稳定性测试

    场景:工厂定制机器,要求一个设备创建wifi热点,一个设备去连接.但是现在发现wifi连接很不稳定,主要以下3方面: 1.连接之前,不容易连接上 2.连接上之后,连不到外网 3.连接上之后,稳定性不好 ...

  8. VC6.0打开或者添加工程文件崩溃的解决方法

    在Win7操作系统下使用Visual C++ 6.0编程时,如果点击菜单中的[打开]或者[添加],或者按快捷键,都会弹出下图的对话框,出现程序崩溃并退出的情况. 出现这种问题的原因是VC6.0和其他软 ...

  9. Entity Framework公共的增删改方法

    using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.I ...

  10. Java多线程——<七>多线程的异常捕捉

    一.概述 为什么要单独讲多线程的异常捕捉呢?先看个例子: public class ThreadException implements Runnable{ @Override public void ...