UK Consulting Links
UK Consulting Blogs
In a recent project I was using regular expressions a lot for validating string expressions such that statements like the following were true:
However in reflection the char data type checks, such as IsDigit(), along with extension methods offer an alternative implementation. What I wanted to write was validations such as:
The implementation to support such expressions is:
This provides a variety of checks all based on the char IsXXX() methods.
The IsAllValidation() is the actual implementation for all extension methods. As a parameter it takes the char based method for which each char in the string is to be checked. This works based on the fact that the String Type supports IEnumerable of Type Char. We can then use the LINQ All<char>() method to ensure that each char in the string satisfies the required condition.
The length check is an optional check specified using the Optional Attribute on the length parameter. If a value greater than zero is specified then the string is also validated to ensure it has the specified length.
As you can see, with a small amount of code one can very easily extend char methods to support checking each char in a string.
Enjoy!
Written by Carl Nolan
Nice Stuff. Sure it'll help.
Thx.
For some F# ramblings on this topic check out: blogs.msdn.com/.../f-string-extension-methods-rather-than-using-regex.aspx