Regular expression examples

0 Likes

Problem:

Regular expression examples for use in creating CodeReview rules.

Resolution:

Regular Expression Examples:

Find a SqlDataReader that was not closed

This is an example of a regular expression that uses the following constructs:

Negative Lookahead

Capture

Multi-line

The problem that is being solved:

In the following code, the SqlDataReader was not closed.

Suppose that we want to create a CodeReview rule that will find the case where a SqlDataReader variable was created but the .Close method was not called for that variable.

public void myMethod2( )
{ 
string mySelectQuery = 'SELECT OrderID, CustomerID FROM Orders'; 
SqlConnection myConnection = new SqlConnection('myConnString'); 
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection); 
myConnection.Open(); 
SqlDataReader myReader; 
myReader = myCommand.ExecuteReader(); 
// Always call Read before accessing data. 
while (myReader.Read()) 
{ 
Console.WriteLine(myReader.GetInt32(0)   ', '   myReader.GetString(1));
} 
// always call Close when done reading.
//myReader.Close(); 
// Close the connection when done with it.
myConnection.Close(); 
} 

The following regular expression will find this coding error.

(?ms)SqlDataReader (?<VARNAME>\w \b)(?!.*\k<VARNAME>\.Close)

Explanation of the regular expression:

(?ms) causes the entire method to be treated as one string (i.e. searches across multiple lines)

SqlDataReader this finds the occurrence of the keyword SqlDataReader followed by one or more spaces

(?<VARNAME>\w \b) this captures the name of the variable and stores it in <VARNAME>

(?!.*\k<VARNAME>\.Close) this is a negative lookahead that will be true if there is no occurrance of

the varable name followed by .Close

Reference:

Mastering Regular Expressions by Jeffrey Friedl

Lookahead (Positive and Negative) page 228

Find an asp:textbox control with no id property

<?XML:NAMESPACE PREFIX = ASP /><ASP:TEXTBOX(?!.*?ID\S*=).*?< asp:textbox>

Find methods that do not have the [AutoComplete(true)] attribute

This is an example of a regular expression that uses the following constructs:

Negative Lookbehind

Multi-line

The regular expression:

(?s)(?<!\[AutoComplete\(true\)\].*?)public \w \w \(

Create a new Code Review rule.

Add a new trigger from this regular expression.

Set the scope of the trigger to Method.

Set the language of the trigger to C# only

Sample Code:

public void myMethod3() // will fire here
{ 
} 
[AutoComplete(false)]
public void myMethod3() // will fire here
{ 
} 
[AutoComplete(true)] 
public void myMethod3() // will not fire here 
{ 
} 

Explanation of the regular expression:

(?s) causes the entire method text to be treated as one continuous string

(?<!\[AutoComplete\(true\)\].*?) finds when [AutoComplete(true)] is missing in this position, which, in this case, is just to the left of the word 'public'

public \w \w \( finds the pattern of a public method declaration in C#

Here are some links that may also help you with regular expressions.

The Perl regular expression rules are very similar to the .NET regular expression rules.

Perl Regular Expressions

http://www.english.uga.edu/humcomp/perl/regex2a.html

Regular Expression HowTo

http://www.amk.ca/python/howto/regex/

Links in MSDN

Regular Expression Language Elements

ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/cpconregularexpressionslanguageelements.htm

Character Escapes

ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/cpconcharacterescapes.htm

Character Classes

ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/cpconcharacterclasses.htm

Regular Expression Options

ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/cpconregularexpressionoptions.htm

Quantifiers

ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/cpconquantifiers.htm

</ASP:TEXTBOX(?!.*?ID\S*=).*?<>

Old KB# 11019
Comment List
Anonymous
Related Discussions
Recommended