Regex in combination with file compare I have made a funtion to compare text files line by line, where timestamps and jobnumbers are replaced for a identical string. By lack of experience I'm not sure if this is the best way to do this.

if (row &lt; comparedFile.Count)
{
    var regexJobNumber = @"([0-9]{4})";
    var regexTimeStamp = @"([0-9]{8}_[0-9]{6})";
    var input = line;
    var removeTimestamp = Regex.Matches(input, regexTimeStamp).Cast&lt;object&gt;().Aggregate(input, (current, Match) =&gt; Regex.Replace(current, regexTimeStamp, "&lt;TimeStamp&gt;"));
    var removeJobnumber = Regex.Matches(removeTimestamp, regexJobNumber).Cast&lt;object&gt;().Aggregate(removeTimestamp, (current, Match) =&gt; Regex.Replace(current, regexJobNumber, "&lt;JobNummer&gt;"));


    var input2 = comparedFile[row];
    var removeTimestamp2 = Regex.Matches(input2, regexTimeStamp).Cast&lt;object&gt;().Aggregate(input2, (current, Match) =&gt; Regex.Replace(current, regexTimeStamp, "&lt;TimeStamp&gt;"));
    var removeJobnumber2 = Regex.Matches(removeTimestamp2, regexJobNumber).Cast&lt;object&gt;().Aggregate(removeTimestamp2, (current, Match) =&gt; Regex.Replace(current, regexJobNumber, "&lt;JobNummer&gt;"));

    if (!removeJobnumber.Equals(removeJobnumber2))
    {
       var rowcount = row + 1;
       differenceList.Add("Difference found on rownumber: " + rowcount);
    }
    row++;
}
This isn't complete code. We have no idea what's happening with row; it seems to be a loop index, but we have no idea what else is happening in the loop.

Anyway, here's my take on things:


Create compiled regexes and move their creation out of the loop, creating Regex instance variables instead of using the static methods. This gets you a speed boost and simplifies the code.
Do the .Replace()s unconditionally; there's no harm in doing so.
Extract the common code into a new function/method. Don't repeat yourself. If you decided you wanted to use the English spelling "Number" instead of Dutch "Nummer" you don't want to have to fix it in two places.


So here it is:

string Standardize(string input)
{
  static var regexJobNumber = new Regex(@"(\d{4})", RegexOptions.Compiled);
  static var regexTimeStamp = new Regex(@"(\d{8}_\d{6})", RegexOptions.Compiled);
  string intermediate = regexTimeStamp.Replace(input, "&lt;TimeStamp&gt;");
  return regexJobNumber.Replace(intermediate,"&lt;JobNumber&gt;");
}

//...

  if (row &lt; comparedFile.Count)
  {
    if (Standardize(removeJobnumber) != Standardize(removeJobnumber2))
    {
       var rowcount = row + 1;
       differenceList.Add("Difference found on rownumber: " + rowcount);
    }
    row++;
  }


My suggestion is to go through the documentation for Regex and see how each method is used.