posts - 234, comments - 480, trackbacks - 56

My Links

News




I am born in Bangladesh and currently live in Melbourne, Australia. I am a co-founder and core developer of Pageflakes www.pageflakes.com and CEO at Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.

I also created SmartCodeGenerator

Some of my articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Making Regex more readable

Making RegEx more readable

Compare the following code statements defining the same regular expression in .NET: static readonly Regex ParameterReference = new Regex(@"(?<empty>\<\>)|\<(?<parameter>[^\<\>]+)\>|(?<open>\<[^\<\>]*(?!\>))",
 RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);


static readonly Regex ParameterReference = new Regex(@"
  # Matches invalid empty brackets #
  (?<empty>\<\>)|
  # Matches a valid parameter reference #
  \<(?<parameter>[^\<\>]+)\>|
  # Matches opened brackes that are not properly closed #
  (?<open>\<[^\<\>]*(?!\>))",
 RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

While the former is still understandable for a fairly regex-aware developer, the later is far more explicit about the purpose of each part of it. The ability to place comments inside the expression is enabled by the RegexOptions.IgnorePatternWhitespace, which is not used enough by developers. In the case of this pretty simple expression this may seem unnecessary, but imagine a regex-based parser that processes (CodeSmith-like) template files:

static Regex CodeExpression = new Regex(@"
  # First match the full directives #
  <\#\s*@\s+(?<directive>\w*)(?<attributes>.*?)\#\/>(?:\W*\n)?|
  # Match open tag #
  (?<open><\#)|
  # Match close tag #
  (?<close>\#\/>)|
  # This is a simple expression that is outputed as-is to output.Write(<output>); #
  (?:=)(?<output>.*?)(?<badmultiple>;.*?)?(?=\#\/>)|
  # Anything previous or after a code tag #
  (?<code>.*?)(?=<\#|\#\/>)|
  # Finally, match everything else that is written as-is #
  (?<snippet>.*[\r\n]*)",
 RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.Singleline);

It's pretty obvious that not commenting such complex expressions makes them almost unreadable except for the guy who wrote them (and even to him after some time!). Bottom line: ALWAYS comment your expressions in-line!!!

Source: http://weblogs.asp.net/cazzu/archive/2004/02/10/70621.aspx

Print | posted on Wednesday, October 12, 2005 9:00 PM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: