Sideway
output.to from Sideway


Built-in Componet


Regular Expression




Draft for Information Only

Content

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

Vb.NET ASPX File:
<%@ Page Language="VB" %>
<!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,"<","&lt;") & """,""" & 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>
HTML Web Page Embedded Output:

See also

  • Regular Expression Language - Quick Reference

Source/Referencee

  • https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
  • https://docs.microsoft.com/en-us/dotnet/standard/base-types/substitutions-in-regular-expressions

©sideway
close

References

  1. Active Server Pages, http://msdn.microsoft.com/en-us/library/aa286483.aspx, updated 20Dec2012
  2. ASP Overview, http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx, updated 21Dec2012
  3. ASP Best Practices, http://technet.microsoft.com/en-us/library/cc939157.aspx, updated 15Apr2013
  4. ASP Built-in Objects, http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx, updated 20Dec2012
  5. Response Object, http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx, updated 20Dec2012
  6. Request Object, http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx, updated 20Dec2012
  7. Server Object (IIS), http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx, updated 20Dec2012
  8. Application Object (IIS), http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx, updated 20Dec2012
  9. Session Object (IIS), http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx, updated 20Dec2012
  10. ASPError Object, http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx, updated 20Dec2012
  11. ObjectContext Object (IIS), http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx, updated 20Dec2012
  12. Debugging Global.asa Files, http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx, updated 15Apr2013
  13. How to: Debug Global.asa files, http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx, updated 15Apr2013
  14. Calling COM Components from ASP Pages, http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx, updated 15Apr2013
  15. IIS ASP Scripting Reference, http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx, updated 15Apr2013
  16. ASP Keywords, http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx, updated 15Apr2013
  17. Creating Simple ASP Pages, http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx, updated 15Apr2013
  18. Including Files in ASP Applications, http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx, updated 15Apr2013
  19. ASP Overview, http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx, updated 15Apr2013
  20. FileSystemObject Object, http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx, updated 14Mayr2013
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx, ADO Object Model, updated 15Apr2013
  22. ADO Fundamentals, http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx, updated 14Mayr2013
close

ID: 190800001 Last Updated: 2019/8/1 Revision: Ref:

IMAGE

Home (5)

Business

Management

HBR (3)

Information

Recreation

Hobbies (7)

Culture

Chinese (1097)

English (336)

Reference (66)

Computer

Hardware (149)

Software

Application (187)

Digitization (24)

Numeric (19)

Programming

Web (648)new

CSS (SC)

ASP.NET (SC)

Regular Expression (SC)

HTML

Knowledge Base

Common Color (SC)

Html Entity (Unicode) (SC)

Html 401 Special (SC)

OS (389)

MS Windows

Windows10 (SC)

.NET Framework (SC)

DeskTop (7)

Knowledge

Mathematics

Formulas (8)

Number Theory (206)

Algebra (20)

Trigonometry (18)

Geometry (18)

Calculus (67)

Complex Analysis (21)

Engineering

Tables (8)

Mechanical

Mechanics (1)

Rigid Bodies

Statics (92)

Dynamics (37)

Fluid (5)

Fluid Kinematics (5)

Control

Process Control (1)

Acoustics (19)

FiniteElement (2)

Biology (1)

Geography (1)


Copyright © 2000-2019 Sideway . All rights reserved Disclaimers last modified on 10 Feb 2019