<<previous next>>

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:

method=put document:[document_name=Shared Documents
/MSF.doc;meta_info=[vti_timelastmodified;TW|09 Sep 2008 10:54:28 -0000]]&

method=remove documents:

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.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:

        string str2 = @"method=put document:
/test&document=[document_name=Shared Documents/MSF.doc;meta_info=
[vti_timelastmodified;TW|09 Sep 2008 10:54:28 -0000]]&put_option=

        string str3 = @"method=remove documents:

        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:
        Dim str2 As String = "method=put document:
/test&document=[document_name=Shared Documents/MSF.doc;meta_info=
[vti_timelastmodified;TW|09 Sep 2008 10:54:28 -0000]]&put_option=
        Dim str3 As String = "method=remove documents:
        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.

Regular expressions

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!

<<previous next>>


Last edited Nov 27, 2009 at 8:46 AM by EugenMakarkhin, version 6


No comments yet.