NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Using the Regular Expression Classes

The classes you will implement to use NGWS frameworks regular expressions are described below. The code fragments are written in C#.

Regex

The Regex class represents an immutable, compiled regular expression. It also contains static methods that allow use of other regular expressions classes without explicitly instantiating objects of the other classes.

The following example declares an object of type Regex and defines a simple regular expression when the Regex object is initialized.

   Regex r; // Declare object variable of type Regex.
   r = new Regex("\\s2000") // Instantiate a Regex object and define
                            // its regular expression.

Match

The Match Class represents the results from a single regular expression matching operation. This example uses the static Match method of the Regex class to find the first match. The Match.Success property is used to test if a match was found.

   Regex r = new Regex("abc"); // Instantiate a new Regex object.
   Match m = r.Match("123abc456"); // Find a single match in the string.
   if (m.Success){
      // Print out character position where match was found 
      // (it will be 3 in this case).
      Console.Write("Found match at position " + m.Index);
   }

MatchCollection

The MatchCollection class represents a sequence of successful nonoverlapping matches. The collection is read-only. This example uses the static Matches method of the Regex class to fill a collection with all matches found in the input string.

   MatchCollection mc;
   int i;
   String[] matchresult = new String[20];
   int[] matchposition[] = new int[20];

   Regex r = new Regex("abc");     // Instantiate a new Regex object and 
                                   // define the regular expression.
   mc = r.Matches("123abc4abcd");  // Use the Matches method to find all 
                                   // matches in the input string.
   // Loop through mc collection to retrieve all matches and positions.
   for (i=0; i<mc.Count; i++) {
      results[i] = mc[i].ToString;    // Add match string to string array.
      matchposition[i] = mc[i].Index; // Record character position where
   }                                  // match was found.

Group

The Group class represents the results from a single capturing group. A capturing group can capture zero, one, or more strings in a single match because of quantifiers, so Group supplies a collection of Capture objects. A Group object is immutable and has no public constructor. Instances are returned by the property Match.Group(groupnum), or Match.Group("groupname") if the (?<groupname>) grouping construct is used.

This example uses nested grouping constructs to capture groups of varying strings. To determine the number of groups returned, count the opening parenthesis, going from left to right.

   String[] results = new String[20];
   r = new Regex("(a(b))c"); // Define three groups: "abc", "ab", and "b".
   m = r.Match("abdababcabcabcabcabc";);
   for (i=0; m.Group(i).ToString() != "" ; i++) {
      results[i]=m.Group(i).ToString();
      matchposition[i] = m.Group(i).Index;
   }

This returns the following:

   results[0] = "abc"
   results[1] = "ab"
   results[2] = "b"

The next example uses named grouping constructs to capture substrings from a string containing data in a "DATANAME:VALUE": format that will be split at the colon (":").

   String inputString="Section1:119900";
   r = new Regex("^(?<name>\\w+):(?<value>\\w+)+");
   m = r.Match(inputString);

This returns the following:

   m.Group("name").ToString() = "Section1"
   m.Group("value").ToString() = "119900"

CaptureCollection

Represents a sequence of captured substrings. The object is used to return the set of captures done by a single capturing group. A static Captures property is provided as a member of the Match and Group classes and will usually be accessed through Match.Captures or Group.Captures.

Capture

The Capture class represents the results from a single subexpression capture. The object represents only one substring for each successful capture.

RegexGroupNameCollection

This collection represents the set of names appearing as capturing group names in a regular expression. The collection is read-only and has no public constructor. Instances are returned using the static GroupNames property in the Regex class. For example:

int namecount;
namecount = r.GroupNames.Count