| You are here: | About>Computing & Technology>Visual Basic |
![]() | Visual Basic |
Compiling Regular ExpressionsNov 17 2007 For Speed, Security, and Better Code OrganizationIn the article Regular Expressions in VB.NET, I show the basics of coding regular expressions in VB.NET. Regular expressions are a 'language in a language' with a history that starts before Visual Basic or even just B.A.S.I.C. and they're used in a lot of programming languages. For this article, I use the "telephone number" RegEx from the article above. Read that article for a more detailed explanation of why it works. Although they're very handy by themselves, regular expressions can be even more useful as compiled DLL modules for all the same reasons that you compile anything: speed, security, and a better way to organize code in libraries. Because it's a 'language in a language', there's no standalone compiler for RegEx in VB.NET. Instead, there's a static method that is part of the normal RegularExpressions namespace. The method is called CompileToAssembly and you will usually call it with two parameters which, naturally enough, are the 'source code' input and the 'compiled assembly' output. (There are also overloaded methods that let you include custom attributes that can be passed to the compiled DLL. The article Attributes in VB .NET explains what attributes are in VB.NET.) To see just how much improvement is possible using compiled regular expressions, this article will show how to compile one. Then the StopWatch component of the System.Diagnostics namespace will be used to compare how fast a regular "inline" execution of the RegEx is versus the same compiled RegEx. First, we have to compile the RegEx. This requirement will probably result in the creation of a utility if you use very many compiled regular expressions. Here's the way I did it. (Arguments in the event Sub's are not shown in this article to save space.) Private Sub CompileRegEx_Click( ... Notice that the actual RegEx is now just a string (instead of being declared as a RegEx as it was in the article referenced earlier). That's because it's passed to the RegexCompilationInfo to be saved as a string property. The illustration below shows the property displayed in the MsgBox. -------- In addition to the actual text of the RegEx, we need to declare ...
Most of this information is simply passed to the New constructor for the RegexCompilationInfo object. The assembly name is used when the RegEx is compiled. One additional detail needs doing. The Regex.CompileToAssembly method actually expects an array of RegexCompilationInfo objects, not just one. The assumption is that you will compile a lot of different regular expressions into the same DLL assembly. That's why this statement is necessary: Dim CompileRegArray() _ Once all this is done, compiling is just a method call: Regex.CompileToAssembly(CompileRegArray, RegExAssembly) On the next page, we use our compiled RegEx and check the speed. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


