SIGN UP MEMBER LOGIN:    
ARTICLE

Using Compiled Regular Expressions

Posted by Kirtan Patel Articles | Current Affairs February 22, 2010
This article explains about how to minimize performance degradation caused by frequently used complex regex using compiled Regular Expressions.
Reader Level:

Introduction

This article explains about how to minimize performance degradation caused by frequently used complex regex using compiled Regular Expressions.

Prerequisites:

C# language,RegEx

Technologies:

.Net 2.0/3.5

Implementation

First of all what is Compiled RegEx???

Well when you create normal RegEx object of complex regex object and want to compare that pattern with some text every time its interpreted by MSIL and then pattern is matched now if we have complex regular expression to match and if it's used frequently it will obvious that it will affect the performance of application.

So to overcome this problem we can use compiled Regular expressions that will be compiled at compile time to MSIL then it will not interpreted again and again in MSIL . That will result in faster execution of regex object.

However several disadvantages also of this compiled time Regex like JIT need to do more work .once application is compiled we cannot unload the Regex .from memory to reclaim it.

Now back to the Point how to create Compiled Regex.

As Example let's make regex Compiled regex object that will match small case word in beginning of string.

Here for making Compiled Regex we need to Set RegexOptions.Compiled option while creating Compiled Regex.

regularexpressions.gif

using System.Text.RegularExpressions;

   private void btnShowMatch_Click(object sender, EventArgs e)

        {

            string InputString = txtInputString.Text;

            Regex rex = new Regex("^[a-z]*", RegexOptions.Compiled);

            Match m = rex.Match(InputString);

            //Show Match in Message Box

            MessageBox.Show(m.Value.ToString());

         }

Conclusion

This article shows how to make use of compiled Regular Expressions to gain performance over non compiled Regular Expressions.

Login to add your contents and source code to this article
share this article :
post comment
 

yes We can use Regular Expressions to Validate User Input .
Some time it can used to Extract Text Patterns that Matches With RegEx Pattern Also for example you want to retrieve onlt email Addresses from some big text file in that case we can use regex another example  -> We can use Regular Expressions to Extract Tags From HTML Source codes also.We can also use regex to implement Syntax High Lighting like visual Studio..

Posted by Kirtan Patel Feb 22, 2010

Yes, I understand the performance issue, but if Match is giving the output as valid characters then how will it be useful validating the user input. As Regular Expressions are used for Validating user input, is it possible to have the validation check using Match.

Posted by Diptimaya Patra Feb 22, 2010

Friend,

Intention of this article is not to show how value is matched and to show
it in Message Box ..but intention of this article is to show How Compiled RegEx can be useful to improve the performance of application.

like when we use normal RegEx object as usual without specifying RegExOptions.Compiled then code will be converted to MSIL at run time.
and if RegEx pattern to be matched is Complex that it can affect the performance of the application while converting to MSIL at Runtime.
so when we use RegExOptions.Compiled option RegEx code will be converted to MSIL at compile time so it will not take time at run time to be converted into MSIL
and that will result in faster execution of code :)

Hope it Solves your doubt :)

Posted by Kirtan Patel Feb 22, 2010

Hey,
I have a small doubt, is it like Match will subset the valid typed text.

What I understood from the above example is like
you entered: kirtan12345
and match is: kirtan

So how is it useful taking the value of Match and displaying in MessageBox. Could you have a good sample.

Posted by Diptimaya Patra Feb 22, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor