Debugging of regular expressions
Let’s consider an example of debugging of regular expression with the help of MAPILab Explorer 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:
C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
using System.Text.RegularExpressions;
public class Tester
{
static void Main(
MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
)
{
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=(?<documentName>[^&;]+)");
Regex regex2 = new Regex(@"(url_list=\[|;)(?<documentName>[^;\]]+)");
// Output browser configuration
//browser.Text = "Browser window";
//browser.DisplayMode =
MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded;
browser.ReturnValue =
new object[] {regex1.Match(str1), regex1.Match(str2), regex2.Matches(str3)};
}
}
Visual Basic
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
Imports System.Text.RegularExpressions
Public Class Tester
Shared Sub Main(ByVal thisForm
As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm,
ByVal browser
As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)
Dim str1
As String = "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"
Dim str2
As String = "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"
Dim str3
As String = "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]"
Dim regex1
As New Regex("[&\[]document_name=(?<documentName>[^&;]+)")
Dim regex2
As New Regex("(url_list=\[|;)(?<documentName>[^;\]]+)")
' Output browser configuration
'browser.Text = "Browser window"
'browser.DisplayMode =
MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded
browser.ReturnValue =
New Object() {regex1.Match(str1), regex1.Match(str2), regex2.Matches(str3)}
End Sub
End Class
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! |
|