WEBSWAPP Silverlight ASP.NET 3.5 ASP.NET 2.0 ASP.NET 1.0
Cascading Lists
ITemplate
DDL in GridView
G/V MultiSelect
Nested GridViews
RegExp
DataGrid Reorder
Events fire twice
Hyperlink Field
TextBox
UpdateParameters
MultiView
TreeView
RegExp-VB

Using named groups in Regular Expressions

In this demo there is an assumed input of an xml document that needs to be converted using a regular expression. The input xml has the following format:
    <responseopt value="5"><someothertag></someothertag></responseopt>
    
The output needs to be in the following format:
    <!--<responseopt value="5">--><someothertag></someothertag><!--<responseopt>-->
    
I will use the following regular expression to identify the pattern
    	\<(\/)?(?<tag>responseopt( value=\""\d\"")?)\>
    
Then I would use the replace function with a replace string parameter that includes the named group in this pattern "<!--<${tag}>-->" Input:

click here to:

Explanation of the Regular Expression
Pattern Description
\< The \ (backslash) metacharacter is used to "escape" characters from their special meaning, as well as to designate instances of predefined set metacharacters. In this pattern it is escaping the < character.
(\/)? ? is a quantifier which describes "0 or 1 occurrence" of the pattern within the brackets
(?<tag>responseopt( value=\""\d\"")?)

This pattern is the syntax for specifying a named group. It specifies the name "tag" for the group. The group name is enclosed in brackets and preceded with a question mark. The group starts with the literals "responseopt". The pattern ( value=\""\d\"")? means that whatever between the brackets can occur 0 or one times. So it would match both the opening and closing xml node tags (the opening has an attribute named "value" with a value equals to a number). The pattern starts with a space then the literals "value=" then one decimal between quotes (notice that in VB you have to repeat the quotation marks to indicate that the quotation mark is part of the string).

For detailed description of the syntax of named groups refer to the following documentation on the MSDN Grouping Constructs.

\> Backslash escaping the < character.
<!--<${tag}>--> This is the pattern that I used as replace paramter passed to the RegExp.Replace function. It is a simple literal, with the syntax ${tag} in its midest; which basically inserts the group named tag (that we scanned in the pattern above) in the midest of the literals

Source code:

RegExp-VB source code