Regular Expression Substitutions Substitution Elements and Replacement Patterns .NET Substitutions Substituting a Numbered Group Substituting a Named Group Substituting a "$" Character Substituting the Entire Match Substituting the Text Before the Match Substituting the Text After the Match Substituting the Last Captured Group Substituting the Entire Input String Examples See also Source/Referencee
Regular Expression Substitutions
Substitutions are language elements that are recognized only within replacement patterns. They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters. Replacement patterns are provided to overloads of the Regex.Replace method that have a replacement parameter and to the Match.Result method. The methods replace the matched pattern with the pattern that is defined by the replacement parameter.
Substitution Elements and Replacement Patterns
Substitutions are the only special constructs recognized in a replacement pattern. None of the other regular expression language elements, including character escapes and the period (.), which matches any character, are supported. Similarly, substitution language elements are recognized only in replacement patterns and are never valid in regular expression patterns.
The only character that can appear either in a regular expression pattern or in a substitution is the $ character, although it has a different meaning in each context. In a regular expression pattern, $ is an anchor that matches the end of the string. In a replacement pattern, $ indicates the beginning of a substitution.
For functionality similar to a replacement pattern within a regular expression, use a backreference.
.NET Substitutions
The supported .NET substitution elements are
Substitution
Description
$ number
Includes the last substring matched by the capturing group that is identified by number, where number is a decimal value, in the replacement string. For more information, see Substituting a Numbered Group.
${ name }
Includes the last substring matched by the named group that is designated by (?<name> ) in the replacement string. For more information, see Substituting a Named Group.
$$
Includes a single "$" literal in the replacement string. For more information, see Substituting a "$" Symbol.
$&
Includes a copy of the entire match in the replacement string. For more information, see Substituting the Entire Match.
$`
Includes all the text of the input string before the match in the replacement string. For more information, see Substituting the Text before the Match.
$'
Includes all the text of the input string after the match in the replacement string. For more information, see Substituting the Text after the Match.
$+
Includes the last group captured in the replacement string. For more information, see Substituting the Last Captured Group.
$_
Includes the entire input string in the replacement string. For more information, see Substituting the Entire Input String.
Substituting a Numbered Group
The $number language element includes the last substring matched by the number capturing group in the replacement string, where number is the index of the capturing group. All digits that follow $ are interpreted as belonging to the number group. If this is not your intent, you can substitute a named group instead.
Capturing groups that are not explicitly assigned names using the (?<name>) syntax are numbered from left to right starting at one. Named groups are also numbered from left to right, starting at one greater than the index of the last unnamed group.
If number does not specify a valid capturing group defined in the regular expression pattern, $number is interpreted as a literal character sequence that is used to replace each match.
Substituting a Named Group
The ${name} language element substitutes the last substring matched by the name capturing group, where name is the name of a capturing group defined by the (?<name>) language element.
If name doesn't specify a valid named capturing group defined in the regular expression pattern but consists of digits, ${name} is interpreted as a numbered group.
If name specifies neither a valid named capturing group nor a valid numbered capturing group defined in the regular expression pattern, ${name} is interpreted as a literal character sequence that is used to replace each match.
Substituting a "$" Character
The $$ substitution inserts a literal "$" character in the replaced string.
Substituting the Entire Match
The $& substitution includes the entire match in the replacement string. Often, it is used to add a substring to the beginning or end of the matched string. For example, the ($&) replacement pattern adds parentheses to the beginning and end of each match. If there is no match, the $& substitution has no effect.
Substituting the Text Before the Match
The $` substitution replaces the matched string with the entire input string before the match. That is, it duplicates the input string up to the match while removing the matched text. Any text that follows the matched text is unchanged in the result string. If there are multiple matches in an input string, the replacement text is derived from the original input string, rather than from the string in which text has been replaced by earlier matches. If there is no match, the $` substitution has no effect.
Substituting the Text After the Match
The $' substitution replaces the matched string with the entire input string after the match. That is, it duplicates the input string after the match while removing the matched text. Any text that precedes the matched text is unchanged in the result string. If there is no match, the $' substitution has no effect.
Substituting the Last Captured Group
The $+ substitution replaces the matched string with the last captured group. If there are no captured groups or if the value of the last captured group is String.Empty, the $+ substitution has no effect.
Substituting the Entire Input String
The $_ substitution replaces the matched string with the entire input string. That is, it removes the matched text and replaces it with the entire string, including the matched text.
Examples
Examples of Substitutions
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script runat="server">
Sub Page_Load()
Dim xstring As String = "zabcd fg ijaab"
Dim xmatchstr As String = ""
xmatchstr = xmatchstr & "Given string: """ & xstring & """<br />"
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","$1")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","$2")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero>\w)(\w)","${zero}")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero1>\w)(?<zero>\w)(\w)","$1")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero>\w)(\w)(?<zero1>\w)","$1")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero>\w)(\w)","$2")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero>\w)(\w)","$3")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero>\w)(\w)","${1}")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero>\w)(\w)","${2}")
xmatchstr = xmatchstr & showresult(xstring,"(?<zero>\w)(\w)","${3}")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","$")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","$$")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","($&)")
xmatchstr = xmatchstr & showresult(xstring,"(\w)00(\w)","($&)")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","($`)")
xmatchstr = xmatchstr & showresult(xstring,"(\w)00(\w)","($`)")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","($')")
xmatchstr = xmatchstr & showresult(xstring,"(\w)00(\w)","($')")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","($+)")
xmatchstr = xmatchstr & showresult(xstring,"(\w)00(\w)","($+)")
xmatchstr = xmatchstr & showresult(xstring,"(\w)(\w)","($_)")
xmatchstr = xmatchstr & showresult(xstring,"(\w)00(\w)","($_)")
lbl01.Text = xmatchstr
End Sub
Function showresult(xstring,xpattern,xreplace)
Dim xmatch As Match
Dim xcaptures As CaptureCollection
Dim ycaptures As CaptureCollection
Dim xgroups As GroupCollection
Dim xmatchstr As String = ""
Dim xint As Integer
Dim yint As Integer
Dim zint As Integer
xmatchstr = xmatchstr & "<br />Result of Regex.Replace(string,""" & Replace(xpattern,"<","<") & """,""" & xreplace & """): """
xmatchstr = xmatchstr & Regex.Replace(xstring,xpattern,xreplace) & """<br />"
xmatch = Regex.Match(xstring,xpattern)
xcaptures = xmatch.Captures
For xint = 0 to xcaptures.Count - 1
xgroups = xmatch.Groups
xmatchstr = xmatchstr & "->Result of GroupCollection.Count: """
xmatchstr = xmatchstr & xgroups.Count & """<br />"
For yint = 0 to xgroups.Count - 1
xmatchstr = xmatchstr & "->->Result of GroupCollection("& yint & ").Value, Index, Length: """
xmatchstr = xmatchstr & xgroups(yint).Value & ", " & xgroups(yint).Index & ", " & xgroups(yint).Length & """<br />"
ycaptures = xgroups(yint).Captures
xmatchstr = xmatchstr & "->->->Result of CaptureCollection.Count: """
xmatchstr = xmatchstr & ycaptures.Count & """<br />"
For zint = 0 to ycaptures.Count - 1
xmatchstr = xmatchstr & "->->->->Result of CaptureCollection("& zint & ").Value, Index, Length: """
xmatchstr = xmatchstr & ycaptures(zint).Value & ", " & ycaptures(zint).Index & ", " & ycaptures(zint).Length & """<br />"
Next
Next
Next
Return xmatchstr
End Function
</script>
</head>
<body>
<% Response.Write ("<h1>This is a Sample Page of Substitutions</h1>") %>
<p>
<%-- Set on Page_Load --%>
<asp:Label id="lbl01" runat="server" />
</p>
</body>
</html>