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