Simple grep in .net


I have been meaning to write a simple grep program which looks through the incoming pipe for a regex and either replaces or matches the lines found for a while.  However I haven’t had enough reason to do it up until now, when I had to process 100 files replacing something which required a zero width look ahead, and it seemed that neither Visual Studio’s integrated search with regex’s or notepad++ could assist me with.

   1: namespace RegexFinder
   2: {
   3:     using System;
   4:     using System.Linq;
   5:     using System.Text.RegularExpressions;
   6:     using System.Diagnostics;
   7:  
   8:     class Program
   9:     {
  10:         static void Main(string[] args)
  11:         {
  12:             if (args.Count() < 1)
  13:             {
  14:                 Console.WriteLine("You should consider giving me a regex to work on, or a pattern and a replacement to replace stuff with the -r switch");
  15:                 return;
  16:             }
  17:  
  18:             string pattern = args[0];
  19:  
  20:             string replace = null;
  21:  
  22:             if (args.Count() > 1 && args[1].StartsWith("-r"))
  23:             {
  24:                 if (args.Count() == 2)
  25:                 {
  26:                     replace = String.Empty;
  27:                 }
  28:                 else
  29:                 {
  30:                     replace = args[2];
  31:                 }
  32:             }
  33:  
  34:             string line = null;
  35:  
  36:             try
  37:             {
  38:                 while ((line = Console.ReadLine()) != null)
  39:                 {
  40:                     if (replace == null)
  41:                     {
  42:                         // do a find
  43:                         if (Regex.IsMatch(line, pattern))
  44:                         {
  45:                             Console.WriteLine(
  46:                                 line
  47:                                 );
  48:                         }
  49:                     }
  50:                     else
  51:                     {
  52:                         // do a replace
  53:                         Console.WriteLine(
  54:                             Regex.Replace(line, pattern, replace)
  55:                             );
  56:                     }
  57:                 }
  58:             }
  59:             catch (ArgumentException ex)
  60:             {
  61:                 Console.WriteLine("You should meditate on the regex, because {0}", ex.Message);
  62:             }
  63:             catch (Exception ex)
  64:             {
  65:                 Console.WriteLine("You should meditate on this error: {0}", ex.Message);
  66:             }
  67:         }
  68:     }
  69: }

This was expected to be a tool only I would use, so didn’t worry too much about the quality of the error handling, however I was quite pleased with the result, especially the parts about reading the pipe coming in.  Now by using Type [inputfilename] | RegexFinder [regex] –r [replacement] > [outputfilename] i can do my replacement with the zero width assertions.

e.g.

type file.txt | grep "(?<=h)e(?=llo)" -r a

will replace all “hello”’s with “hallo”’s.

Then taking that forward to patterns with predefined groups (?<groupname>[regex]) and then the lookup (\k<groupname>) makes it very powerful and very flexible.

Comments (1)

  1. esa says:

    Friends dont miss it please

    esp. USA,UK and German friends

    http://adf.ly/1q5di

Skip to main content