Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace Searcher
- {
- public class Searcher
- {
- static void Main(string[] args)
- {
- //String fileName = @"C:\Documents and Settings\9chat73\Desktop\count.txt";
- // To search dinamically, just ask for a file:
- Console.WriteLine("Enter a file to search");
- String fileName = Console.ReadLine().Trim();
- if (File.Exists(fileName))
- {
- var readOnlyFile = File.ReadAllText(fileName);
- Console.WriteLine("===============================");
- foreach (var words in readOnlyFile)
- {
- Console.Write(words);
- }
- Console.WriteLine("===============================");
- Console.WriteLine("Type a word to search");
- String pattern = "\\b";
- pattern += Console.ReadLine();
- pattern += "\\b";
- // Do not forget to escape the pattern!
- int count = Regex.Matches(File.ReadAllText(fileName),
- pattern,
- RegexOptions.IgnoreCase).Count;
- // IgnoreCase = ignore case sens
- Console.WriteLine(count);
- }
- else
- {
- Console.WriteLine("File Not Found");
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement