Provides a regular expression to parse an ASP.NET expression block.
Namespace:
System.Web.RegularExpressions
Assembly:
System.Web.RegularExpressions (in System.Web.RegularExpressions.dll)
Visual Basic (Declaration)
Public Class AspExprRegex _
Inherits Regex _
Implements ISerializable
Dim instance As AspExprRegex
public class AspExprRegex : Regex,
ISerializable
public ref class AspExprRegex : public Regex,
ISerializable
public class AspExprRegex extends Regex implements ISerializable
The AspExprRegex class provides a compiled regular expression to parse an ASP.NET expression 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 AspExprRegex class is \G<%\s*?=(?<code>.*?)?%>. This regular expression returns an entire inline expression block, including its beginning and ending tags and all of its contents. However, for a match to occur, the expression block must start at the beginning of the input stream. For multiple expression blocks to be found in a call to the AspExprRegex object's Matches method, they must be immediately contiguous to one another. That is, the %> tag that terminates the previous expression block must be immediately followed by the <%= tag that begins the next expression block, without any intervening characters.
You can retrieve all inline expression blocks regardless of their position in an input string in either of two ways:
By instantiating a Regex object whose regular expression pattern is <%\s*?=(?<code>.*?)?%>.
By using the String..::.IndexOf(String, Int32) overload to locate the next opening tag of an inline expression block in a string, and then using this position when calling the AspExprRegex object's Match(String, Int32) method. The following example illustrates how to use this technique with the AspExprRegex object to extract all the inline expression blocks in a string.
Dim simpleStrings() As String = {"<%= DisplayValue() %>", _
" <%= DisplayValue() %>", _
"<%= DisplayValue() %><%= DisplayValue() %>", _
"<%= DisplayValue() %> <%= DisplayValue() %>"}
Dim aspExpr As New AspExprRegex()
Console.WriteLine("Pattern: {0}", aspExpr.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 = aspExpr.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<%\s*?=(?<code>.*?)?%>
'
' Input: <%= DisplayValue() %>
' '<%= DisplayValue() %>'
' Input: <%= DisplayValue() %>
' '<%= DisplayValue() %>'
' Input: <%= DisplayValue() %><%= DisplayValue() %>
' '<%= DisplayValue() %>'
' '<%= DisplayValue() %>'
' Input: <%= DisplayValue() %> <%= DisplayValue() %>
' '<%= DisplayValue() %>'
' '<%= DisplayValue() %>'
string[] simpleStrings = {"<%= DisplayValue() %>",
" <%= DisplayValue() %>",
"<%= DisplayValue() %><%= DisplayValue() %>",
"<%= DisplayValue() %> <%= DisplayValue() %>"};
AspExprRegex aspExpr = new AspExprRegex();
Console.WriteLine("Pattern: {0}", aspExpr.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 = aspExpr.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<%\s*?=(?<code>.*?)?%>
//
// Input: <%= DisplayValue() %>
// '<%= DisplayValue() %>'
// Input: <%= DisplayValue() %>
// '<%= DisplayValue() %>'
// Input: <%= DisplayValue() %><%= DisplayValue() %>
// '<%= DisplayValue() %>'
// '<%= DisplayValue() %>'
// Input: <%= DisplayValue() %> <%= DisplayValue() %>
// '<%= DisplayValue() %>'
// '<%= DisplayValue() %>'
The following example demonstrates the AspExprRegex class. The example illustrates that an inline expression block matches the regular expression pattern only if it is at the beginning of a string or if it immediately follows the preceding match.
Dim simpleStrings() As String = {"<%= DisplayValue() %>", _
" <%= DisplayValue() %>", _
"<%= DisplayValue() %><%= DisplayValue() %>", _
"<%= DisplayValue() %> <%= DisplayValue() %>"}
Dim aspExpr As New AspExprRegex()
Console.WriteLine("Pattern: {0}", aspExpr.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 = aspExpr.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<%\s*?=(?<code>.*?)?%>
'
' Input String: '<%= DisplayValue() %>'
' <%= DisplayValue() %>
' Input String: ' <%= DisplayValue() %>'
' No match.
' Input String: '<%= DisplayValue() %><%= DisplayValue() %>'
' <%= DisplayValue() %>
' <%= DisplayValue() %>
' Input String: '<%= DisplayValue() %> <%= DisplayValue() %>'
' <%= DisplayValue() %>
string[] simpleStrings = {"<%= DisplayValue() %>",
" <%= DisplayValue() %>",
"<%= DisplayValue() %><%= DisplayValue() %>",
"<%= DisplayValue() %> <%= DisplayValue() %>"};
AspExprRegex aspExpr = new AspExprRegex();
Console.WriteLine("Pattern: {0}", aspExpr.ToString());
Console.WriteLine();
// Iterate string array
foreach (string simpleString in simpleStrings)
{
Console.WriteLine("Input String: '{0}'", simpleString);
// Get substrings matching regex pattern
MatchCollection matches = aspExpr.Matches(simpleString);
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<%\s*?=(?<code>.*?)?%>
//
// Input String: '<%= DisplayValue() %>'
// <%= DisplayValue() %>
// Input String: ' <%= DisplayValue() %>'
// No match.
// Input String: '<%= DisplayValue() %><%= DisplayValue() %>'
// <%= DisplayValue() %>
// <%= DisplayValue() %>
// Input String: '<%= DisplayValue() %> <%= DisplayValue() %>'
// <%= DisplayValue() %>
System..::.Object
System.Text.RegularExpressions..::.Regex
System.Web.RegularExpressions..::.AspExprRegex
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