Geeks With Blogs
HanSolo code monkey
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);

Posted on Monday, December 1, 2008 7:23 PM | Back to top

Comments on this post: Regex Performance Tip

# re: Regex Performance Tip
Requesting 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;


#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);


#region Private Static Member Data

/// <summary>
/// The regular expression to test the string against.
/// </summary>
private static readonly Regex m_validEmail =
new Regex (
+ @"(\.[^<>()[\]\\.,;:\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,}))$",


Left by Jesse C. Slicer on Dec 05, 2008 2:56 PM

Your comment:
 (will show your gravatar)

Copyright © HanSolo | Powered by: