Click here to Skip to main content
15,353,719 members
Articles / Programming Languages / C#
Posted 6 Jun 2005


92 bookmarked

Regular Expression Library Builder

Rate me:
Please Sign up or sign in to vote.
4.63/5 (24 votes)
14 Jun 2005CPOL7 min read
A tool to quickly allow the creation and modification of .NET Regular Expression Libraries


Regular Expressions are an immensely powerful tool in a developer’s arsenal. The flavour of regexs and the classes surrounding them that are available in the .NET Framework is, in my humble opinion, excellent. One of the most useful methods of all the methods available on all the types in the System.Text.RegularExpressions namespace is the static CompileToAssembly method of the Regex class. The CompileToAssembly method allows you to compile a regex to a standalone assembly - not much good you might think. Well, ponder on the problem I was left with:

The Problem

I was developing, actually I still am developing, an application that does quite a bit of text processing and makes a lot of use of regular expressions. Now regular expressions are complex, mysterious creatures. You may think you know lots about them, but trust me, you never know as much as you can until you have read Mastering Regular Expressions, 2nd Edition [^] a few hundred times :-).

I don't believe for one second that a regular expression that I write will be efficient the first time around. Sure, it will do exactly what I want it to do, but will it do it in as efficient a way as possible? Probably not. So, I needed to make extensive use of regular expressions and I knew that I didn't have the time to spend, or rather didn't want to spend the time, measuring the performance of every regex I wrote straightaway and then spending lots of time making it as efficient as possible.... so I was left with a problem: how do I easily replace the regexs within my application with new, more efficient regexs when I needed them? I didn't want to move them to a config file, as they are too easy to modify by the end user, and loading and parsing XML is, relatively speaking, a slow enough operation.

The Solution

A "Regular Expression Library," CompileToAssembly came to my rescue. I wrote a tool to quickly allow me to easily add multiple regular expressions to a "Regular Expression Library." I can create new libraries, load and modify existing ones, quickly add, remove, modify regular expressions and then redistribute the "Regular Expression Library" assembly via an auto-update to the application. The "parent" application can get new über-efficient regular expressions and continue working without any user intervention if needs be.

The RegexLibrary Builder main screen

Figure 1: The RegexLibrary Builder Main Screen

The RegexLibrary Builder

The RegexLibrary builder allows you to:

  • Create CLS-compliant Regular Expression Libraries: .NET assemblies that contain only regular expressions.
  • Add multiple regular expressions to a single assembly.
  • Define individual names, namespaces, regex modifiers and accessibility levels on a per regex basis.
  • Reload existing Regular Expression Libraries and add, remove or modify regular expressions contained within.
  • Manually set the version number of the assembly to help ensure compatibility with existing versions.
  • Much, much, more... ;-)

Seriously though, I wrote this tool because I really needed it. I'm not sure if other people will, but if you do, you can download it from above. Remember that this tool was put together quickly and for my own use, so don't expect any fancy exception handling, nice coding patterns, comments, unit tests or anything like that. ;-) It will have bugs... I'd appreciate it if you could leave a comment here or contact me to let me know if/when you find one/many. :-)

If people are interested in the tool, I will develop it further and add things such as the ability to sign the Regular Expression Libraries that have been created, the ability to add custom attributes on a per regex basis, add some exception handling, code comments and so on. Also, if you use the tool, let me know! It's always good to know if something you've created for your own use is useful for others too.

Creating and Modifying a Regular Expression Library

The RegexLibrary Builder is an easy-to-use tool and most people should be able to figure it out without much difficulty. However, just in case, I'll write a quick (very quick) overview of how to create a Regular Expression Library and then how to open it again and modify it.

Creating a Regular Expression Library

  1. Open up the RegexLibrary Builder application... obviously. ;-)

    Steps 2 and 3 are interchangeable - i.e. order doesn't matter.

  2. Fill in the assembly details (Figure 2). You can use the "..." button to select the location where the Regular Expression Library will be saved to. Fill in the assembly details

    Figure 2: Fill in the Assembly Details

  3. Create a new regular expression to add to the Regular Expression Library by filling in the details in the regex group box and then clicking on "Add" (Figure 3). Check out the Regulator [^] and RegexDesigner.NET [^], two very cool regular expression testing and learning tools. Add a regular expression to the Regular Expression Library

    Figure 3: Add a Regular Expression to the Regular Expression Library

    The regex will now appear in the list of regular expressions in the list box at the bottom of the application.

  4. Finally, click on the "Save" button on the toolbar or select "Save Regex Library" from the "File" menu.

Modifying a Regular Expression Library

  1. Load the Regular Expression Library by clicking on the "Open" button on the toolbar or by selecting "Load Regex Library" from the "File" menu.
  2. When the library loads, all the regular expressions contained within will be displayed in the list box at the bottom of the application (Figure 4). The regular expressions loaded from a Regular Expression Library.

    Figure 4: The Regular Expressions Loaded from a Regular Expression Library

  3. When you click on a regular expression in the list, its details will be filled in in the regex group box. To modify the regex, simply change the details and click on "Add" again. You can delete a regex by highlighting it and then clicking on the "Delete" button, clicking on "Delete" on the toolbar or by selecting "Delete Regex" on the "Regex" menu.
  4. When you are happy with the changes you've made, you can save the changes to the Regular Expression Library by clicking on the "Save" button on the toolbar or selecting "Save Regex Library" from the "File" menu.

Using a Regular Expression Library

Once you have created a Regular Expression Library, using it in one of your applications is straightforward:

  1. Add a reference to the assembly, as you would any other assembly.
  2. Add the using statement for the namespace of the regex you want. Remember that you can set the namespace on a per regex basis if you like.
  3. Use the regex as you would the Regex class:
    // Check to see if some text matches
    bool result = DutchPostCode.IsMatch("some text");
    // Get all the matches
    MatchCollection matches = DutchPostCode.Matches(new 
    // Basically, just use it as you would the Regex Class

The Source

The source is straightforward. Nothing complicated. As I mentioned above, I wrote this tool quickly and for myself. I didn't expect to release the source, so you won't find any fancy exception handling, nice coding patterns, comments, unit tests or anything like that. ;-) It will have bugs... I'd appreciate it if you could leave a comment here or contact me to let me know if/when you find one/many. :-)

The interesting source code file is RegexLibraryBuilder.cs, which contains the RegexLibraryBuilder class and the nested RegexLibraryLoader class. There is also a strongly typed RegexCompilationInfo collection class (cleverly named RegexCompilationInfoCollection ;-) with your boiler plate strongly typed collection code. The RegexLibraryBuilderForm class is the implementation of the UI and, before someone points it out, I know that it isn't very modular or well-designed. I should have moved certain aspects to a custom control and provided property access to certain things, as well as moved certain things out to separate classes. However, the app was thrown together quickly.


Two very cool regular expression testing and learning tools:

Other useful links:

  • RegexLib [^], an online library of regular expressions: I grabbed the three regexs in the screen shot in Figure 1 from there.
  • Regular Expressions [^] on MSDN
  • Original posting of the RegexLibrary Builder on my blog [^]


  • June 14th, 2005 - Added support for validating regexs before saving them: now you cannot save invalid regexs.
  • June 8th, 2005 - Updated formatting.
  • June 6th, 2005 - First posted.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Brian Delahunty
Software Developer (Senior)
United States United States
Irish guy living in Seattle who is passionate about technology, running, trail running. Software Engineer @Microsoft by day (and night too Smile | :) )

He is an ex Codeproject Editor... time restrictions are still a problem.

Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey8-Feb-12 19:58
professionalManoj Kumar Choubey8-Feb-12 19:58 
NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés1-Mar-08 23:16
MemberPablo Osés1-Mar-08 23:16 
GeneralCommand line update and file path problem Pin
Greg0101013-Sep-07 9:45
MemberGreg0101013-Sep-07 9:45 
I updated the code slightly so it takes the library name as a command line parameter. If it exists it will open the file automatically, if it doesn't it just prefills the file name in the form and that's it. It's useful because now I can set it as a tool in VS and not hunt for the file each time. If you want it say so and I'll post the code.

A problem I've been having is that it seems to ignore the path and always puts the output file in the working directory. It happens in the unmodified version too, and I can't seem to get around it. I've tried putting a file:/// prefix on AssemblyName.CodeBase like the docs say, changing slashes to backslashes, etc. and nothing works. It's not a huge deal as I've been working around it by making the working directory the same as the file's, just thought I'd point it out. I'm using Vista if it matters.
Questionwhta will be the regular expression. Pin
biswa472-Aug-07 21:16
Memberbiswa472-Aug-07 21:16 
GeneralIsMatch Arguments Pin
seesharpme21-Jun-07 6:41
Memberseesharpme21-Jun-07 6:41 
GeneralRe: IsMatch Arguments Pin
Brian Delahunty21-Jun-07 20:53
MemberBrian Delahunty21-Jun-07 20:53 
QuestionReg Ex Pin
stixoffire18-Jun-07 12:29
Memberstixoffire18-Jun-07 12:29 
Generalhelp with Unicode characters Pin
shweta.shrivastava24-Jan-07 0:11
Membershweta.shrivastava24-Jan-07 0:11 
GeneralError Pin
Dr.Strangelove13-Jan-07 6:28
MemberDr.Strangelove13-Jan-07 6:28 
Generalintegration with VB.NET Pin
W Miles15-May-06 18:35
MemberW Miles15-May-06 18:35 
GeneralLatest Update Pin
twesterd25-Mar-06 8:35
Membertwesterd25-Mar-06 8:35 
AnswerRe: Latest Update Pin
Brian Delahunty25-Mar-06 13:17
MemberBrian Delahunty25-Mar-06 13:17 
GeneralRe: Latest Update Pin
twesterd25-Mar-06 18:39
Membertwesterd25-Mar-06 18:39 
GeneralStrong Name missing Pin
Anonymous5-Jul-05 11:12
MemberAnonymous5-Jul-05 11:12 
GeneralRe: Strong Name missing Pin
Brian Delahunty6-Jul-05 23:38
MemberBrian Delahunty6-Jul-05 23:38 
GeneralOther Assembly Attributes Pin
DietmarS28-Jun-05 1:43
MemberDietmarS28-Jun-05 1:43 
GeneralRe: Other Assembly Attributes Pin
Brian Delahunty28-Jun-05 4:19
MemberBrian Delahunty28-Jun-05 4:19 
GeneralRe: Other Assembly Attributes Pin
Peter Gummer2-Oct-14 2:11
MemberPeter Gummer2-Oct-14 2:11 
GeneralRun from command-line Pin
RossDonald8-Jun-05 15:24
MemberRossDonald8-Jun-05 15:24 
GeneralRe: Run from command-line Pin
Brian Delahunty8-Jun-05 22:16
MemberBrian Delahunty8-Jun-05 22:16 
GeneralRe: Run from command-line Pin
RossDonald8-Jun-05 22:27
MemberRossDonald8-Jun-05 22:27 
GeneralRe: Run from command-line Pin
Brian Delahunty8-Jun-05 22:34
MemberBrian Delahunty8-Jun-05 22:34 
GeneralExcellent ! Pin
BillWoodruff6-Jun-05 21:02
mveBillWoodruff6-Jun-05 21:02 
GeneralRe: Excellent ! Pin
Brian Delahunty7-Jun-05 1:18
MemberBrian Delahunty7-Jun-05 1:18 
GeneralCool Pin
Uwe Keim6-Jun-05 9:56
sitebuilderUwe Keim6-Jun-05 9:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.