c#计算txt文件中发生的特定单词

时间:2016-10-26 03:32:16

标签: c# console

    static void Main(string[] args)
    {
        StreamReader oReader;
        if (File.Exists(@"C:\cmd.txt"))
        {
            Console.WriteLine("IMAGE");
            string cSearforSomething = Console.ReadLine().Trim();
            oReader = new StreamReader(@"C:\cmd.txt");
            string cColl = oReader.ReadToEnd();
            string cCriteria = @"\b" + cSearforSomething + @"\b";
            System.Text.RegularExpressions.Regex oRegex = new System.Text.RegularExpressions.Regex(cCriteria, RegexOptions.IgnoreCase);



            int count = oRegex.Matches(cColl).Count;
            Console.WriteLine(count.ToString());
        }
        Console.ReadLine();
    }

我无法在我的文件中计算字符串“IMAGE”发生的次数?我的代码错了吗?

3 个答案:

答案 0 :(得分:0)

根据https://msdn.microsoft.com/en-us/library/az24scfc.aspx,“\ b”匹配退格。这可能解释了为什么你不匹配正则表达式。

答案 1 :(得分:0)

试试此代码

List<String> myList = new ArrayList();
SolrDocumentList results = response.getResults();
    for (int i = 0; i < results.size(); ++i) {
      System.out.println(results.get(i));
      myList.add(results.get(i));
    }

答案 2 :(得分:0)

使用System; 使用System.Text.RegularExpressions;

命名空间ConsoleApplication8 {     课程     {

    static void Main(string[] args)
    {
        string cColl = System.IO.File.ReadAllText(@"C:\some.txt");
        //string cColl = "This is similar, similar, similar, similar, similar, similar";
        Console.WriteLine(cColl);

        string cCriteria = @"\b" + "similar" + @"\b";
        Regex oRegex = new Regex(cCriteria, RegexOptions.IgnoreCase);
        int count = oRegex.Matches(cColl).Count;
        Console.WriteLine(count.ToString());

        Console.ReadLine();
    }




}

}