用正则表达式怎么做类似word的统计字数,来着有分

CSharp_s 2011-03-03 09:30:22
求用正则表达式怎么做类似word的统计字数
要求统计 英文单词 汉字数 字符数(计空格) 字符数(不计空格) 四项
要效率高的,给个代码,分不够我再加。
...全文
789 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSharp_s 2011-03-04
  • 打赏
  • 举报
回复
谢谢了,我就是研究不懂正则表达式
-过客- 2011-03-04
  • 打赏
  • 举报
回复
举一反三,[a-z_'-]自己往里加,不过MS也没什么可加的了

            string test = @"测试,一下。     one line;
And ano-
ther line.
ssss_ss we'are
空格 制表符
";
richTextBox1.Text = test;
richTextBox2.Text += "英文单词数:".PadRight(10) + Regex.Matches(test, @"(?i)[a-z_'-]+").Count + "\n";
richTextBox2.Text += "汉字数:".PadRight(10) + Regex.Matches(test, @"[\u0391-\uFFE5]").Count + "\n";
richTextBox2.Text += "字符数(计空格):".PadRight(10) + Regex.Matches(test, @"[^\r\n]").Count + "\n";
richTextBox2.Text += "字符数(不计空格):".PadRight(10) + Regex.Matches(test, @"[^\s]").Count + "\n";
CSharp_s 2011-03-04
  • 打赏
  • 举报
回复
英文单词还有'也算一个怎么写,比如we'are 算一个
-过客- 2011-03-03
  • 打赏
  • 举报
回复
try...

            string test = @"测试,一下。     one line;
And ano-
ther line.
ssss_ss
空格 制表符
";
richTextBox2.Text += "英文单词数:".PadRight(10) + Regex.Matches(test, @"(?i)[a-z_-]+").Count + "\n";
richTextBox2.Text += "汉字数:".PadRight(10) + Regex.Matches(test, @"[\u0391-\uFFE5]").Count + "\n";
richTextBox2.Text += "字符数(计空格):".PadRight(10) + Regex.Matches(test, @"[^\r\n]").Count + "\n";
richTextBox2.Text += "字符数(不计空格):".PadRight(10) + Regex.Matches(test, @"[^\s]").Count + "\n";
CSharp_s 2011-03-03
  • 打赏
  • 举报
回复
还用中文汉字帮我把标点也算上
CSharp_s 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lxcnn 的回复:]
下面这样统计的结果是和word一致的,看下如果还有不一致的,给下例子


C# code
string test = @"测试一下 one line
And ano-
ther line
空格 制表符
";
richTextBox2.Text += "英文单词数:".PadRig……
[/Quote]

谢了,统计英文单词的时候和word的不同,可以帮我再改一下吗? 比如word中ssss_ss是算一个单词
叶子 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lxcnn 的回复:]
下面这样统计的结果是和word一致的,看下如果还有不一致的,给下例子


C# code
string test = @"测试一下 one line
And ano-
ther line
空格 制表符
";
richTextBox2.Text += "英文单词数:".PadRig……
[/Quote]
小客客的正则依旧犀利...
-过客- 2011-03-03
  • 打赏
  • 举报
回复
下面这样统计的结果是和word一致的,看下如果还有不一致的,给下例子

            string test = @"测试一下     one line
And ano-
ther line
空格 制表符
";
richTextBox2.Text += "英文单词数:".PadRight(10) + Regex.Matches(test, @"(?i)[a-z-]+").Count + "\n";
richTextBox2.Text += "汉字数:".PadRight(10) + Regex.Matches(test, @"[\u4e00-\u9fa5]").Count + "\n";
richTextBox2.Text += "字符数(计空格):".PadRight(10) + Regex.Matches(test, @"[^\r\n]").Count + "\n";
richTextBox2.Text += "字符数(不计空格):".PadRight(10) + Regex.Matches(test, @"[^\s]").Count + "\n";
CSharp_s 2011-03-03
  • 打赏
  • 举报
回复
谢谢3楼的,英文单词我解决了

继续求 汉字数 字符数(计空格) 字符数(不计空格)这三项正则怎么写
zjx198934 2011-03-03
  • 打赏
  • 举报
回复
System.Text.RegularExpressions.Regex.Matches(s, @"\w+").Count 匹配所有的数字和字母以及下划线
System.Text.RegularExpressions.Regex.Matches(s, @"\W+").Count 匹配除数字、字母外及下划线外的其它字符

还有很多正则的匹配符号 可以在这儿看!
CSharp_s 2011-03-03
  • 打赏
  • 举报
回复
可以说详细一点吗?
这2个是统计什么的
wuyq11 2011-03-03
  • 打赏
  • 举报
回复
先读取内容
System.Text.RegularExpressions.Regex.Matches(s, @"\w+").Count
System.Text.RegularExpressions.Regex.Matches(s, @"[\u4E00-\u9FA5]").Count

111,083

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧