Provides a regular expression to parse an ASP.NET code block.
Namespace:
System.Web.RegularExpressions
Assembly:
System.Web.RegularExpressions (in System.Web.RegularExpressions.dll)
Visual Basic (Declaration)
Public Class AspCodeRegex _
Inherits Regex _
Implements ISerializable
Dim instance As AspCodeRegex
public class AspCodeRegex : Regex,
ISerializable
public ref class AspCodeRegex : public Regex,
ISerializable
public class AspCodeRegex extends Regex implements ISerializable
The AspCodeRegex class provides a compiled regular expression to parse an ASP.NET code block (<% … %>).
Note: |
|---|
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 AspCodeRegex class is \G<%(?!@)(?<code>.*?)%>. This regular expression returns an entire inline code block, including its beginning and ending tags and all of its contents. However, for a match to occur, the code block must start at the beginning of the input stream. For multiple inline code blocks to be found in a call to the AspCodeRegex object's Matches method, they must be immediately contiguous to one another. That is, the %> tag that terminates the previous inline code block must be immediately followed by the <% tag that begins the next inline code block, without any intervening characters.
Note: |
|---|
In addition to returning inline code blocks, an AspCodeRegex object also returns inline expression blocks (which are delimited by <%= … %> tags) and data-binding expressions (which are delimited by <%# … %> tags). |
You can retrieve all inline code blocks regardless of their position in an input string in either of two ways:
By instantiating a Regex object whose regular expression pattern is <%(?!@)(?<code>.*?)%>.
By using the String..::.IndexOf(String, Int32) overload to locate the next occurrence of a code render block in a string, and then using this position when calling the AspCodeRegex object's Match(String, Int32) method. The following example illustrates how to use this technique with the AspCodeRegex object to extract all the inline code blocks in a string.
Dim simpleStrings() As String = {"<% DoSomething() %>", _
" <% DoSomething() %>", _
"<% DoSomething() %><% DoSomething() %>", _
"<% DoSomething() %> <% DoSomething() %>"}
Dim aspCode As New AspCodeRegex()
Console.WriteLine("Pattern: {0}", aspCode.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 = aspCode.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<%(?!@)(?<code>.*?)%>
'
' Input: <% DoSomething() %>
' '<% DoSomething() %>'
' Input: <% DoSomething() %>
' '<% DoSomething() %>'
' Input: <% DoSomething() %><% DoSomething() %>
' '<% DoSomething() %>'
' '<% DoSomething() %>'
' Input: <% DoSomething() %> <% DoSomething() %>
' '<% DoSomething() %>'
' '<% DoSomething() %>'
string[] simpleStrings = {"<% DoSomething() %>",
" <% DoSomething() %>",
"<% DoSomething() %><% DoSomething() %>",
"<% DoSomething() %> <% DoSomething() %>"};
AspCodeRegex aspCode = new AspCodeRegex();
Console.WriteLine("Pattern: {0}", aspCode.ToString());
Console.WriteLine();
// Iterate string array
foreach (string simpleString in simpleStrings)
{
Console.WriteLine("Input: {0}", simpleString);
int position = 0;
List<Match> matches = new List<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)
{
Match match = aspCode.Match(simpleString, 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<%(?!@)(?<code>.*?)%>
//
// Input: <% DoSomething() %>
// '<% DoSomething() %>'
// Input: <% DoSomething() %>
// '<% DoSomething() %>'
// Input: <% DoSomething() %><% DoSomething() %>
// '<% DoSomething() %>'
// '<% DoSomething() %>'
// Input: <% DoSomething() %> <% DoSomething() %>
// '<% DoSomething() %>'
// '<% DoSomething() %>'
The following example demonstrates the AspCodeRegex class. The example illustrates that an inline code block matches the regular expression only if it is at the beginning of a string or if it immediately follows the preceding match.
Dim simpleStrings() As String = {"<% DoSomething() %>", _
" <% DoSomething() %>", _
"<% DoSomething() %><% DoSomething() %>", _
"<% DoSomething() %> <% DoSomething() %>"}
Dim aspCode As New AspCodeRegex()
Console.WriteLine("Pattern: {0}", aspCode.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 = aspCode.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:
' Input String: '<% DoSomething() %>'
' <% DoSomething() %>
' Input String: ' <% DoSomething() %>'
' No match.
' Input String: '<% DoSomething() %><% DoSomething() %>'
' <% DoSomething() %>
' <% DoSomething() %>
' Input String: '<% DoSomething() %> <% DoSomething() %>'
' <% DoSomething() %>
string[] simpleStrings = {"<% DoSomething() %>",
" <% DoSomething() %>",
"<% DoSomething() %><% DoSomething() %>",
"<% DoSomething() %> <% DoSomething() %>"};
AspCodeRegex aspCode = new AspCodeRegex();
Console.WriteLine("Pattern: {0}", aspCode.ToString());
Console.WriteLine();
foreach (string simpleString in simpleStrings)
{
Console.WriteLine("Input String: '{0}'", simpleString);
MatchCollection matches = aspCode.Matches(simpleString);
if (matches.Count > 0)
{
foreach (Match match in matches)
Console.WriteLine(" {0}", match.Value);
}
else
{
Console.WriteLine(" No match.");
}
}
// This example displays the following output to the console:
// Input String: '<% DoSomething() %>'
// <% DoSomething() %>
// Input String: ' <% DoSomething() %>'
// No match.
// Input String: '<% DoSomething() %><% DoSomething() %>'
// <% DoSomething() %>
// <% DoSomething() %>
// Input String: '<% DoSomething() %> <% DoSomething() %>'
// <% DoSomething() %>
System..::.Object
System.Text.RegularExpressions..::.Regex
System.Web.RegularExpressions..::.AspCodeRegex
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
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.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference