Regex Performance Tip

Here is something that you may not be aware of. In .NET, if you use regular expressions, your code would perform much better if you minimize the times when you instantiate a Regex object.

For example, if you have a regular expression that you use repetitively throughout your code, you should construct the Regex object once and reuse it throughout your code. You'd be surprised how much overhead the Regex constructor requires.

Additionally, you should always pre-compile your regular expressions as well.

Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled);






  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Monday, December 01, 2008 7:23 PM
Print

Comments on this entry:

# re: Regex Performance Tip

Left by Jesse C. Slicer at 12/5/2008 2:56 PM
Gravatar
Indeed I do love me regular expressions - and because of the "construct it once" mentality, I've taken to using them statically as much as possible. And that also lends them to being used in extension methods nicely:

namespace Aesop.Extensions
{
#region Using Directives

// System namespaces
using System.Text.RegularExpressions;

#endregion

#region Static Class Definition : EmailValidatorExtension

/// <summary>
/// Holds the IsValidEmailAddress () extension method.
/// </summary>
public static class EmailValidatorExtension
{
#region Public Static Extension Methods

/// <summary>
/// Determines whether the specified string is a valid email address.
/// </summary>
/// <param name="value">
/// The string to validate.
/// </param>
/// <returns>
/// <c>true</c> if the specified string is a valid email address;
/// otherwise, <c>false</c>.
/// </returns>
public static bool
IsValidEmailAddress (this string value)
{
return m_validEmail.IsMatch (value);
}

#endregion

#region Private Static Member Data

/// <summary>
/// The regular expression to test the string against.
/// </summary>
private static readonly Regex m_validEmail =
new Regex (
@"^(([^<>()[\]\\.,;:\s@\""]+"
+ @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
+ @"[a-zA-Z]{2,}))$",
RegexOptions.Compiled);

#endregion
}

#endregion
}

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910