Castle windsor converter for Regex
In previous post I explained how to build a Converter for castle, to make possibile specify regular expression in configuration file. The example is simplicistic, let’s see how to extend to create a better converter for Regular Expression.
The main problem is that RegularExpression is not only defined by the pattern but also by regular expression option. I decided to use this syntax
{pattern},{comma separated option}
so a possible definition is
{<a\s*href=”(?<uri>[^”]*?)” rel=”bookmark”},{Singleline,IgnoreCase}
This is a regex I use to find some particular data in html page, as you can see the first part is the regex, then I specify the option Singleline and IgnoreCase. Let’s rewrite the conversion function to accept this new syntax.
|
|
I first create a simple Regex that split the two group, first of all I recreate the correct option, to do this I simply split the option part and use Enum.Parse to recreate correct values from string description. For each option I use the or= operator (RegexOptions is a Flag enum) and the game is done. Now I’m able to specify both pattern and options in Castle configuration file
alk.
Tags: Castle Windsor