Debugging of regular expressions
Let’s consider an example of debugging of regular expression with the help of MAPILab Spy for SharePoint.
As initial data, requests to FrontPage Server Extensions will be taken into consideration. These are the examples of basic requests sent from Microsoft Word to Microsoft Office SharePoint Server 2007:
method=get document:12.0.0.4518&service_name=&document_name=Lists/Links/Untitled_2.css& old_theme_html=false&force=true&get_option=none&doc_version=&timeout=0&expandWebPartPages=true
method=put document:6.0.2.6551&service_name=/test&document=[document_name=Shared Documents /MSF.doc;meta_info=[vti_timelastmodified;TW|09 Sep 2008 10:54:28 -0000]] &put_option=edit&comment=&keep_checked_out=false
method=remove documents:12.0.0.4518&service_name=&url_list= [Lists/Links/Untitled_2.css;Lists/Links/Untitled_1.css;Lists/Links /Untitled_3.css;Lists/Links/Untitled_4.css]
Our task is to create regular expressions for searching names of documents mentioned in the requests above as well as to analyze the results.
Let’s open a new code window and change the text of the main method in the following way:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
public class Tester
{
static object main(
MAPILab.SharePoint.Spy.CodeForm thisForm
)
{
string str1 = @"method=get document:12.0.0.4518&service_name= &document_name=Lists/Links/Untitled_2.css&old_theme_html=false&force=true &get_option=none&doc_version=&timeout=0&expandWebPartPages=true";
string str2 = @"method=put document:6.0.2.6551&service_name= /test&document=[document_name=Shared Documents/MSF.doc;meta_info= [vti_timelastmodified;TW|09 Sep 2008 10:54:28 -0000]]&put_option= edit&comment=&keep_checked_out=false";
string str3 = @"method=remove documents:12.0.0.4518&service_name= &url_list=Lists/Links/Untitled_2.css;Lists/Links/Untitled_1.css;Lists/Links/ Untitled_3.css;Lists/Links/Untitled_4.css]";
Regex regex1 = new Regex(@"[&\[]document_name=(?[^&;]+)");
Regex regex2 = new Regex(@"(url_list=\[|;)(?[^;\]]+)");
return new object[] {regex1.Match(str1), regex1.Match(str2), regex2.Matches(str3)};
}
}
For the first two cases, one and the same regular expression regex1 can be used in this script. The third case is more difficult than the previous ones as the initial data contains several names of the document. A separate regular expression regex2 was specially created for it.
In order to check the correctness of functioning of these regular expressions let’s return their results in the form of object array. This object array will be shown in the browser of the object model.
Click to open real size screenshot
If we open the nodes as it shown in the picture, we can see that all regular expressions were created correctly. Now you need only to copy them into the source code of the commercial product as we did it while we were developing MAPILab Statistics for SharePoint! |