比如
string str="傻子|笨蛋"
string content="从前有一个傻子和一个笨蛋在一起玩耍,傻子吗笨蛋是笨蛋";想用正则提取傻子和笨蛋出现的次数
傻子出现2次,笨蛋出现3次有难度啊
string str="傻子|笨蛋"
string content="从前有一个傻子和一个笨蛋在一起玩耍,傻子吗笨蛋是笨蛋";想用正则提取傻子和笨蛋出现的次数
傻子出现2次,笨蛋出现3次有难度啊
解决方案 »
- webbrowser获取cookie,自动登录的图片验证
- 两个combobox中的选项移动(C# 项为DataSource)
- 小问题,请问C#中的源程序如何换行?
- 向存储过程传递常量时不能使用 OUTPUT 选项。
- 从字符串数组 a[]=new char[20] 拷贝字符串到 b[] = new char[10] 怎么拷贝?
- [100分]求Visual Studio Tools for Office 2003中文版
- 我要用一辈子的时间去疼你、爱你时,你却放弃了~~~
- 在WinForm中打开XP可视化样式后,出现在问题
- 求改个C#函数为断点续传?
- 那位大哥能讲一下MapX!!!
- 关于sql查询的问题 大家帮忙看下
- 程序运行时报错
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;namespace ConsoleApplication1
{
class Program
{
string content = "从前有一个傻子和一个笨蛋在一起玩耍,傻子吗笨蛋是笨蛋";
//因为傻子和笨蛋都是两个词,所以位置是紧靠一起的
char[] fool1 = new char[2] {'傻','子' };
char[] fool2 = new char[2] { '笨', '蛋' }; public Program()
{
int fool1Count = 0;
int fool2Count = 0; //arrayList中记录了整段话的所有数据
char[] arrayList = content.ToCharArray();
//接下来对每个字依次对比
for (int i = 0; i < arrayList.Length -1; i++)
{
if (arrayList[i] == fool1[0] && arrayList[i+1]==fool1[1])
{
fool1Count++;
}
if (arrayList[i] == fool2[0] && arrayList[i + 1] == fool2[1])
{
fool2Count++;
}
}
//fool1Count代表“傻子”的次数,fool2Count代表“笨蛋”的次数
Console.WriteLine("fool1 times={0},fool2 times={1}", fool1Count, fool2Count);
} static void Main()
{
Program p1 = new Program(); Console.ReadLine();
}
}}
string content = "从前有一个傻子和一个笨蛋在一起玩耍,傻子吗笨蛋是笨蛋";
Match m = Regex.Match(content , @"(?<sz>傻子)|(?<bd>笨蛋)");
m.Groups["sz"].Captures.Count;
m.Groups["bd"].Captures.Count;
private static int GetStringCount(string input, string compare)
{
int index = input.IndexOf(compare);
if (index != -1)
{
return 1 + GetStringCount(input.Substring(index + compare.Length), compare);
}
else
{
return 0;
} }
Match m = Regex.Match(content, @"(?s)(?:(傻子)|(笨蛋)|(?!傻子|笨蛋).)+");
richTextBox2.Text += "\"傻子\"出现次数:" + m.Groups[1].Captures.Count + "\n";
richTextBox2.Text += "\"笨蛋\"出现次数:" + m.Groups[2].Captures.Count + "\n";