Skip to main content
shorter form of "any digit"
Source Link
Snowbody
  • 8.5k
  • 23
  • 50

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(@"([0-9]\d{4})", RegexOptions.Compiled);
  static var regexTimeStamp = new Regex(@"([0-9]\d{8}_[0-9]_\d{6})", RegexOptions.Compiled);
  string intermediate = regexTimeStamp.Replace(input, "<TimeStamp>");
  return regexJobNumber.Replace(intermediate,"<JobNumber>");
}

//...

  if (row < 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.

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(@"([0-9]{4})", RegexOptions.Compiled);
  static var regexTimeStamp = new Regex(@"([0-9]{8}_[0-9]{6})", RegexOptions.Compiled);
  string intermediate = regexTimeStamp.Replace(input, "<TimeStamp>");
  return regexJobNumber.Replace(intermediate,"<JobNumber>");
}

//...

  if (row < 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.

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, "<TimeStamp>");
  return regexJobNumber.Replace(intermediate,"<JobNumber>");
}

//...

  if (row < 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.

Source Link
Snowbody
  • 8.5k
  • 23
  • 50

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(@"([0-9]{4})", RegexOptions.Compiled);
  static var regexTimeStamp = new Regex(@"([0-9]{8}_[0-9]{6})", RegexOptions.Compiled);
  string intermediate = regexTimeStamp.Replace(input, "<TimeStamp>");
  return regexJobNumber.Replace(intermediate,"<JobNumber>");
}

//...

  if (row < 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.