![]() |
![]() |
|
November 26, 2003Powers of Regex() in C#Well today has been a good coding day. I just refactored a section of my code from just over 900 lines to that of just under 100, thanks to C#'s Regex() class. I am so bloody proud of that I just had to tell the world. Well more to the point, I wanted to blog it so others can learn from my mistakes. I won't go directly into my code, but will quickly state that it was 900 lines of lexical analyzer goodness... or badness.. depending on who is reading it. I was reading in a custom configuration file and trying to break it down into smaller tokens so I can deal with it. The parser was huge... mostly because this code was ported from C where I HEAVILY used pointers to deal with the slide and compare routines. Now enough about my code... and onto why C# Regex() rocks. I don't need to discuss WHY Regex() is important (I have done that before, and you got that you need to treat all input as malcious until validated otherwise RIGHT???).. but I want to teach you about a neat little feature that makes it a $DEITY send. It's called named groupings. With it, when the regular expressions are ran through... it will take the named group construct and capture substrings if and when they match. What is nice about this approach is that you can use it find an exact pattern match, and then break it down into its child substrings directly without having to parse it out. Pretty sweet if you ask me! Everything is stored in the resulting Match.Groups[] array, which can be queried by passing the named group. The construction of a named group is quite easy.... its just (?<named_group>expression). Let me show you a simple example of how to use this. Lets say you want to parse out a simple line that holds a string value, and then a numeric ulong value which is in circle brackets. ie: foo(1) Here is how you would do that:
Ya ya ya... I should be catching the Parse() exception... but that was not added for clarity. You get the idea though. Within a few lines you got properly validated data directly through the Regex()! Now... lets make this even easier. I found a sweet tool from Rad Software that is perfect for building these regular expressions with named groupings called RegEx Designer and its FREE! It allows you to quickly test different regex and see the results immediately. Thanks for the tool guys! All and all this has made my day. Any time you can reduce the amount of code and thus reduce the potential bug surface... you are having a great day. Especially when I shrunk it by a factor of 9 times! And its quite easy to review and manage... which makes it all the more interesting. So if you haven't had a chance to check it out... give it a try. Regex() and "named groupings"... a great combination! Posted by SilverStr at November 26, 2003 03:59 PM | TrackBackComments
interesting, on many levels... I was googling around looking for some Regex examples in c# and found this log... interesting in that its pretty much the regex feature i was curious about and this log was entered on my birthday... so thanks for the present Posted by: Jeff at December 21, 2003 09:43 AMHey, I'm glad it was useful to you! And happy belated birthday! :) Posted by: SilverStr at December 21, 2003 11:09 AMGreat little article :) |
![]() ![]()
My 5 Favorite Books
Writing Secure Code
Secure Programming Cookbook Security Engineering Secure Coding Principles & Practice Inside the Security Mind ![]()
My 5 Favorite Papers
Smashing the Stack
Penetration Studies Covert Channel Analysis of Trusted Systems DoD Trusted Computer System Evaluation Criteria NSA Security Recommendation Guides ![]()
Archives
December 2005
November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 July 2003 June 2003 May 2003 April 2003 March 2003 February 2003 January 2003 December 2002 November 2002 October 2002 September 2002 August 2002 July 2002 ![]() |
|