.NET Framework Class Library
CommentRegex Class

Provides a regular expression to parse an ASP.NET comment block.

Namespace:  System.Web.RegularExpressions
Assembly:  System.Web.RegularExpressions (in System.Web.RegularExpressions.dll)
Syntax

Visual Basic (Declaration)
Public Class CommentRegex _
    Inherits Regex _
    Implements ISerializable
Visual Basic (Usage)
Dim instance As CommentRegex
C#
public class CommentRegex : Regex, 
    ISerializable
Visual C++
public ref class CommentRegex : public Regex, 
    ISerializable
JScript
public class CommentRegex extends Regex implements ISerializable
Remarks

The CommentRegex class provides a compiled regular expression to parse an ASP.NET comment block (<%----%>).

NoteNote:

The System.Web.RegularExpressions classes are meant to be used by the .NET Framework to parse ASP.NET pages and are not always practical for general applications. For example, many of these classes match only the beginning of a string.

The predefined regular expression pattern used by the CommentRegex class is \G<%--(([^-]*)-)*?-%>. This regular expression returns the entire server-side comment block, including its beginning and ending tags and all of its contents. However, for a match to occur, the opening comment tag must start at the beginning of the input stream. For multiple comments to be found in a call to the CommentRegex object's Matches method, they must be immediately contiguous to one another. That is, the --%> tag that terminates the previous comment must be immediately followed by the <%-- tag that begins the next comment, without any intervening characters.

You can retrieve all comment blocks regardless of their position in an input string in either of two ways:

  • By instantiating a Regex object whose regular expression pattern is <%--(([^-]*)-)*?-%>.

  • By using the String..::.IndexOf(String, Int32) overload to locate the next opening tag of a comment block in a string, and then using this position when calling the CommentRegex object's Match(String, Int32) method. The following example illustrates how to use this technique with the CommentRegex object to extract all the inline expression blocks in a string.

    Visual Basic
    Dim simpleStrings() As String = {"<%-- This is a comment --%>", _
                                     " <%-- This is a comment --%>", _
                                     "<%-- This is a comment --%><%-- This is a comment --%>", _ 
                                     "<%-- This is a comment --%> <%-- This is a comment --%>"}
    Dim commentReg As New CommentRegex()        
    Console.WriteLine("Pattern: {0}", commentReg.ToString())
    Console.WriteLine()
    
    ' Iterate string array
    For Each simpleString As String In simpleStrings
       Console.WriteLine("Input: {0}", simpleString)
       Dim position As Integer = 0
       Dim matches As New List(Of Match)
    
       Do 
          ' Get next occurrence of beginning code block tag
          position = simpleString.IndexOf("<%--", position)
          ' If it exists, pass this portion of string to regex engine
          If position > -1 Then 
             Dim match As Match = commentReg.Match(simpleString, position)
             ' Add successful match to collection
             If match.Success Then
                matches.Add(match)
             End If
             position += 1
          End If   
       Loop While position > -1
    
       ' Iterate collection of matches
       For Each match As Match In matches
          Console.WriteLine("   '{0}'", match.Value)
       Next   
    Next
    ' This example displays the following output to the console:
    '       Pattern: \G<%--(([^-]*)-)*?-%>
    '       
    '       Input: <%-- This is a comment --%>
    '          '<%-- This is a comment --%>'
    '       Input:  <%-- This is a comment --%>
    '          '<%-- This is a comment --%>'
    '       Input: <%-- This is a comment --%><%-- This is a comment --%>
    '          '<%-- This is a comment --%>'
    '          '<%-- This is a comment --%>'
    '       Input: <%-- This is a comment --%> <%-- This is a comment --%>
    '          '<%-- This is a comment --%>'
    '          '<%-- This is a comment --%>'
    
    C#
    string[] commentStrings = {"<%-- This is a comment --%>", 
                               " <%-- This is a comment --%>", 
                               "<%-- This is a comment --%><%-- This is a comment --%>",  
                               "<%-- This is a comment --%> <%-- This is a comment --%>"};
    CommentRegex commentReg = new CommentRegex();        
    Console.WriteLine("Pattern: {0}", commentReg.ToString());
    Console.WriteLine();
    
    // Iterate string array
    foreach (string commentString in commentStrings)
    {
       Console.WriteLine("Input: {0}", commentString);
       int position = 0;
       List<Match> matches = new List<Match>();
    
       do
       { 
          // Get next occurrence of beginning code block tag
          position = commentString.IndexOf("<%--", position);
          // If it exists, pass this portion of string to regex engine
          if (position > -1)
          { 
             Match match = commentReg.Match(commentString, position);
             // Add successful match to collection
             if (match.Success)
                matches.Add(match);
             position++;
          }   
       } while (position > -1);
    
       // Iterate collection of matches
       foreach (Match match in matches)
          Console.WriteLine("   '{0}'", match.Value);
    }
    // This example displays the following output to the console:
    //       Pattern: \G<%--(([^-]*)-)*?-%>
    //       
    //       Input: <%-- This is a comment --%>
    //          '<%-- This is a comment --%>'
    //       Input:  <%-- This is a comment --%>
    //          '<%-- This is a comment --%>'
    //       Input: <%-- This is a comment --%><%-- This is a comment --%>
    //          '<%-- This is a comment --%>'
    //          '<%-- This is a comment --%>'
    //       Input: <%-- This is a comment --%> <%-- This is a comment --%>
    //          '<%-- This is a comment --%>'
    //          '<%-- This is a comment --%>'
    
Examples

The following example demonstrates the CommentRegex class. The example illustrates that a comment block matches the regular expression pattern only if it is at the beginning of a string or if it immediately follows the preceding match.

Visual Basic
Dim simpleStrings() As String = {"<%-- This is a comment --%>", _
                                 " <%-- This is a comment --%>", _
                                 "<%-- This is a comment --%><%-- This is a comment --%>", _ 
                                 "<%-- This is a comment --%> <%-- This is a comment --%>"}
Dim commentReg As New CommentRegex()
Console.WriteLine("Pattern: {0}", commentReg.ToString())
Console.WriteLine()

' Iterate string array
For Each simpleString As String In simpleStrings
   Console.WriteLine("Input String: '{0}'", simpleString)
   ' Get substrings matching regex pattern
   Dim matches As MatchCollection = commentReg.Matches(simpleString)
   If matches.Count > 0 Then
      ' Display matches to console
      For Each match As Match In matches
         Console.WriteLine("   {0}", match.Value)
      Next
   Else
      Console.WriteLine("   No match.")
   End If
Next   
' This example displays the following output to the console:
'       Pattern: \G<%--(([^-]*)-)*?-%>
'       
'       Input String: '<%-- This is a comment --%>'
'          <%-- This is a comment --%>
'       Input String: ' <%-- This is a comment --%>'
'          No match.
'       Input String: '<%-- This is a comment --%><%-- This is a comment --%>'
'          <%-- This is a comment --%>
'          <%-- This is a comment --%>
'       Input String: '<%-- This is a comment --%> <%-- This is a comment --%>'
'          <%-- This is a comment --%>
C#
string[] commentStrings = {"<%-- This is a comment --%>", 
                           " <%-- This is a comment --%>", 
                           "<%-- This is a comment --%><%-- This is a comment --%>",  
                           "<%-- This is a comment --%> <%-- This is a comment --%>"};
CommentRegex commentReg = new CommentRegex();
Console.WriteLine("Pattern: {0}", commentReg.ToString());
Console.WriteLine();

// Iterate string array
foreach (string commentString in commentStrings)
{
   Console.WriteLine("Input String: '{0}'", commentString);
   // Get substrings matching regex pattern
   MatchCollection matches = commentReg.Matches(commentString);
   if (matches.Count > 0)
   {
      // Display matches to console
      foreach (Match match in matches)
         Console.WriteLine("   {0}", match.Value);
   }
   else
   {
      Console.WriteLine("   No match.");
   }
}   
// This example displays the following output to the console:
//       Pattern: \G<%--(([^-]*)-)*?-%>
//       
//       Input String: '<%-- This is a comment --%>'
//          <%-- This is a comment --%>
//       Input String: ' <%-- This is a comment --%>'
//          No match.
//       Input String: '<%-- This is a comment --%><%-- This is a comment --%>'
//          <%-- This is a comment --%>
//          <%-- This is a comment --%>
//       Input String: '<%-- This is a comment --%> <%-- This is a comment --%>'
//          <%-- This is a comment --%>
Inheritance Hierarchy

System..::.Object
  System.Text.RegularExpressions..::.Regex
    System.Web.RegularExpressions..::.CommentRegex
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker