Assembly: System.Web.RegularExpressions (in system.web.regularexpressions.dll)
The AspCodeRegex class provides a compiled regular expression to parse an ASP.NET code block (<% … %>).
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.
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 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 method. The following example illustrates how to use this technique with the AspCodeRegex object to extract all the inline code blocks in a string.
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.