In our Maine Microsoft Certification Study Group we recently had a discussion about using regular expression. Today I found myself writing a RegEx to check for illegal characters in a formula (string). I thought I’d share the solution:
private bool FormulaContainsIllegalCharacters( string formula )
{
bool result = false;
try
Regex r = new Regex( @"(!)|(@)|(#)|(\$)|(%)|(&)" );
result = r.Match( formula ).Success;
}
catch { } // ignore any regular expressions errors -> return false
return result;
In my case I’m not interested in handling exceptions. If a technical error occurs I will accept the input. Notice that I needed to put a “\” before the $ sign, since the $ is a reserved character marking the end of a line.I don’t need to put each character in “( )” brackets, but for personal preference I just find it easer to read.
dasBlog theme by Mads Kristensen
Concepts LINQ Entity Framework WCF WPF RESTful Web Unit Testing .NET Workflow More >>
Tools Visual Studio Windows IIS Silverlight More >>
Type Screencast Tools Video Newsletter Sample Article Books Magazine How To Demo Course Products More >>