<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Matt Manela's Blog : Regular Expression</title><link>http://blogs.msdn.com/matt/archive/tags/Regular+Expression/default.aspx</link><description>Tags: Regular Expression</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Inline Regular Expression Options</title><link>http://blogs.msdn.com/matt/archive/2009/01/01/inline-regular-expression-options.aspx</link><pubDate>Thu, 01 Jan 2009 23:10:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9263718</guid><dc:creator>MattManela</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/matt/comments/9263718.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=9263718</wfw:commentRss><description>&lt;p&gt;I was using attributes from the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx" target="_blank"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt;&amp;#160;&amp;#160; namespace for model validation.&amp;#160; This namespace includes a few very useful validation attributes such as&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Required Attribute – Validates the field has a value &lt;/li&gt;      &lt;li&gt;Range Attribute – Validates the field is within a given range &lt;/li&gt;      &lt;li&gt;RegularExpression Attribute – Validates the field matches a given regular expression. &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The regular expression attribute is very useful since you can describe exactly what format you want a string property to be in.&amp;#160; While using this though I ran into a problem.&amp;#160; The attribute doesn’t let you specify RegexOptions.&amp;#160; This was an issue for me since I wanted to use the regex to validate that the users input was between 5 and 200 characters long , so I had attribute that property as such:&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; [RegularExpression(&lt;span style="color: #006080"&gt;@&amp;quot;.{5,200}&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Text{get;set;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;However this doesn’t work since by default the wildcard . does not match new lines (which I am allowing in the input).&amp;#160; The way to fix this is to specify the RegexOptions.SingleLine option to either the Regex constructor or Match function.&amp;#160; The problem is I have no way of doing that here, and there is no argument on the attribute constructor to specify those options.&amp;#160; I was considering overriding the attribute to create one that allows specifying the attribute but then I stumbled upon this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msdn.microsoft.com/library/yd1hzczs.aspx"&gt;Regular Expression Options&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You are able to specify the regex option inside of the regular expression text! (which I thought was a huge discovery until my co-worker said he knew this all along but never let me know!).&amp;#160; &lt;/p&gt;

&lt;p&gt;So I just changed the expression to look like this:&lt;/p&gt;

&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; [RegularExpression(&lt;span style="color: #006080"&gt;@&amp;quot;(?s).{5,200}&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Text{get; set;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The (?s) is the inline regex option definition so say I want this in SingleLine mode! And now the validation works the way I wanted!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9263718" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/matt/archive/tags/Regular+Expression/default.aspx">Regular Expression</category></item></channel></rss>