This project is read-only.

FxC2202 : RegularExpressionDoesNotCompile

TypeName RegularExpressionDoesNotCompile
CheckId FxC2202
Category FxCopContrib.Usage
Breaking Change Non-breaking


An expression value is being passed to a Regex object which will result in a Runtime error.

Rule Description

Regular Expressions are not checked by the compiler and will thus result in runtime errors if they are invalid. This rule attempts to find constant strings which are being passed to a Regex object or DataAnnotation. The Expression is then parsed and any errors are reported.

How to fix violations

Use a tool such as Regulator or Regular Expression Designer to test your Regular Expressions before you put them in the code.

When to suppress warnings

This rule attempts to find all constant strings, but it might get confused when a expression is being built up in that method.


You've hard-coded a regular expression in your code, and it contains an error.

Bad Example
public bool BadMethod(string value)
     return Regex.IsMatch(value, "[a-z]*+"); // will result in an ArgumentException at runtime.

Good Example
public void GoodMethod(string value)
     return Regex.IsMatch(value, "[a-z]+");


This rule uses a static list of ImmutableTypes. If you want to add your own ImmutableTypes, you can do so by creating a file named ImmutableTypes.txt in the folder which contains FxCop.exe or in the folder that contains your FxCop project file.

Last edited Dec 1, 2011 at 10:45 PM by jessehouwing, version 2


No comments yet.