返回介绍

正则表达式中的替代

发布于 2025-02-23 23:15:43 字数 22391 浏览 0 评论 0 收藏 0

替换是只能在替换模式中识别的语言元素。 它们使用正则表达式模式定义全部或部分用于替换输入字符串中的匹配文本的文本。 替换模式可以包含一个或多个替换以及本文字符。 提供替换模式以将拥有 Regex.Replace 参数的 replacement 方法重载至 Match.Result 方法。 该方法将匹配的模式替换为 replacement 参数定义的模式。

.NET Framework 定义下表列出的替换元素。

替换说明
$ 数值包括替换字符串中的由 number标识的捕获组所匹配的最后一个子字符串,其中 number 是一个十进制值。 有关详细信息,请参阅 替换已编号的组
${ name }包括替换字符串中由 (?<name> ) 指定的命名组所匹配的最后一个子字符串。 有关详细信息,请参阅 替换命名组
$$包括替换字符串中的单个 $ 文本。 有关详细信息,请参阅 替换 $ 符号
$&包括替换字符串中整个匹配项的副本。 有关详细信息,请参阅 替换整个匹配项
$`包括替换字符串中的匹配项前的输入字符串的所有文本。 有关详细信息,请参阅 替换匹配项前的文本
$'包括替换字符串中的匹配项后的输入字符串的所有文本。 有关详细信息,请参阅 替换匹配项后的文本
$+包括在替换字符串中捕获的最后一个组。 有关详细信息,请参阅 替换最后捕获的组
$_包括替换字符串中的整个输入字符串。 有关详细信息,请参阅 替换整个输入字符串

替换元素和替换模式

替换是替换模式中唯一可识别的特殊构造。 与任何字符匹配的其他正则表达式语言元素(包括字符转义和句点 ( . ))均不受支持。 同样,替换语言元素只能在替换模式中识别,并且在正则表达式模式中永远无效。

可以出现在正则表达式模式或替换中的唯一字符是 $ 字符,尽管它在每个上下文中具有不同的含义。 在正则表达式模式中, $ 是与字符串的末尾匹配的定位点。 在替换模式中, $ 指示替换的开头。

备注

对于类似于正则表达式中替换模式的功能,使用反向引用。 有关反向引用的更多信息,请参见 反向引用构造 。

替换已编号的组

$number 语言元素包括替换字符串中 number 捕获组所匹配的最后一个子字符串,其中 number 是捕获组的索引。 例如,替换模式 $1 指示匹配的子字符串将由捕获的第一个组替换。 有关为已编号的捕获组的详细信息,请参见 Grouping Constructs 。

$ 后面的所有数字解释为属于 number 组。 如果这不是你想要的结果,可改为替换命名组。 例如,可以使用替换字符串 ${1}1 而不是 $11 来将替换字符串定义为带数字 1 的首个捕获组的值。 有关详细信息,请参阅 替换命名组

没有使用 (?<name>) 语法显式分配的名称的捕获组从左到右进行编号(从 1 开始)。 命名组还从左到右进行编号,从比最后一个未命名组的索引大 1 的值开始。 例如,在正则表达式 (\w)(?<digit>\d) 中, digit 命名组的索引为 2。

如果 number 未指定在正则表达式模式中定义的有效捕获组, $number 将被解释为用于替换每个匹配项的文本字符序列。

下面的示例使用 $number 替换去除十进制值中的货币符号。 它移除在货币值的开头或末尾找到的货币符号,并识别两个最常见的小数点分隔符( ., )。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    string pattern = @"\p{Sc}*(\s?\d+[.,]?\d*)\p{Sc}*";
    string replacement = "$1";
    string input = "$16.32 12.19 £16.29 €18.29  €18,29";
    string result = Regex.Replace(input, pattern, replacement);
    Console.WriteLine(result);
   }
}
// The example displays the following output:
//     16.32 12.19 16.29 18.29  18,29
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    Dim pattern As String = "\p{Sc}*(\s?\d+[.,]?\d*)\p{Sc}*"
    Dim replacement As String = "$1"
    Dim input As String = "$16.32 12.19 £16.29 €18.29  €18,29"
    Dim result As String = Regex.Replace(input, pattern, replacement)
    Console.WriteLine(result)
   End Sub
End Module
' The example displays the following output:
'     16.32 12.19 16.29 18.29  18,29

正则表达式模式 \p{Sc}*(\s?\d+[.,]?\d*)\p{Sc}* 的定义如下表所示。

模式说明
\p{Sc}*与零个或多个货币符号字符匹配。
\s?匹配零个或一个空白字符。
\d+匹配一个或多个十进制数字。
[.,]?与零个或一个句点或逗号匹配。
\d*匹配零个或多个十进制数字。
(\s?\d+[.,]?\d*)匹配空白,后跟一个或多个十进制数,再后跟零个或一个句点或逗号,后跟零个或多个十进制数。 这是第一个捕获组。 因为替换模式为 $1 ,所以调用 Regex.Replace 方法会将整个匹配的子字符串替换为此捕获组。

返回页首

替换命名组

${name} 语言元素替换 name 捕获组匹配的最后一个子字符串,其中 name(?<name>) 语言元素所定义的捕获组名称。 有关命名的捕获组的详细信息,请参见 Grouping Constructs 。

如果 name 未指定正则表达式模式中定义的有效的命名捕获组但包含数字,则 ${name} 被解释为已编号的组。

如果 name 既未指定有效的命名捕获组也未指定正则表达式模式中定义的有效的已编号捕获组,则 ${name} 被解释为用于替换每个匹配的文本字符序列。

下面的示例使用 ${name} 替换去除十进制值中的货币符号。 它移除在货币值的开头或末尾找到的货币符号,并识别两个最常见的小数点分隔符( ., )。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    string pattern = @"\p{Sc}*(?<amount>\s?\d+[.,]?\d*)\p{Sc}*";
    string replacement = "${amount}";
    string input = "$16.32 12.19 £16.29 €18.29  €18,29";
    string result = Regex.Replace(input, pattern, replacement);
    Console.WriteLine(result);
   }
}
// The example displays the following output:
//     16.32 12.19 16.29 18.29  18,29
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    Dim pattern As String = "\p{Sc}*(?<amount>\s?\d+[.,]?\d*)\p{Sc}*"
    Dim replacement As String = "${amount}"
    Dim input As String = "$16.32 12.19 £16.29 €18.29  €18,29"
    Dim result As String = Regex.Replace(input, pattern, replacement)
    Console.WriteLine(result)
   End Sub
End Module
' The example displays the following output:
'     16.32 12.19 16.29 18.29  18,29

正则表达式模式 \p{Sc}*(?<amount>\s?\d[.,]?\d*)\p{Sc}* 的定义如下表所示。

模式说明
\p{Sc}*与零个或多个货币符号字符匹配。
\s?匹配零个或一个空白字符。
\d+匹配一个或多个十进制数字。
[.,]?与零个或一个句点或逗号匹配。
\d*匹配零个或多个十进制数字。
(?<amount>\s?\d[.,]?\d*)匹配空白,后跟一个或多个十进制数,再后跟零个或一个句点或逗号,后跟零个或多个十进制数。 这是名为 amount 的捕获组。 因为替换模式为 ${amount} ,所以调用 Regex.Replace 方法会将整个匹配的子字符串替换为此捕获组。

返回页首

替换 $ 字符

$$ 替换将在替换的字符串中插入文本 $ 字符。

下面的示例使用 NumberFormatInfo 对象确定当前区域性的货币符号及其在货币字符串中的位置。 然后,它动态构建一个正则表达式模式和一个替换模式。 如果示例在当前区域性为 en-US 的计算机上运行,则它将生成正则表达式模式 \b(\d+)(\.(\d+))? 和替换模式 $$ $1$2 。 替换模式将匹配的文本替换为一个后跟第一个和第二个捕获组的货币符号和空格。

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    // Define array of decimal values.
    string[] values= { "16.35", "19.72", "1234", "0.99"};
    // Determine whether currency precedes (True) or follows (False) number.
    bool precedes = NumberFormatInfo.CurrentInfo.CurrencyPositivePattern % 2 == 0;
    // Get decimal separator.
    string cSeparator = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
    // Get currency symbol.
    string symbol = NumberFormatInfo.CurrentInfo.CurrencySymbol;
    // If symbol is a "$", add an extra "$".
    if (symbol == "$") symbol = "$$";
    
    // Define regular expression pattern and replacement string.
    string pattern = @"\b(\d+)(" + cSeparator + @"(\d+))?"; 
    string replacement = "$1$2";
    replacement = precedes ? symbol + " " + replacement : replacement + " " + symbol;
    foreach (string value in values)
     Console.WriteLine("{0} --> {1}", value, Regex.Replace(value, pattern, replacement));
   }
}
// The example displays the following output:
//     16.35 --> $ 16.35
//     19.72 --> $ 19.72
//     1234 --> $ 1234
//     0.99 --> $ 0.99
Imports System.Globalization
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    ' Define array of decimal values.
    Dim values() As String = { "16.35", "19.72", "1234", "0.99"}
    ' Determine whether currency precedes (True) or follows (False) number.
    Dim precedes As Boolean = (NumberFormatInfo.CurrentInfo.CurrencyPositivePattern Mod 2 = 0)
    ' Get decimal separator.
    Dim cSeparator As String = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator
    ' Get currency symbol.
    Dim symbol As String = NumberFormatInfo.CurrentInfo.CurrencySymbol
    ' If symbol is a "$", add an extra "$".
    If symbol = "$" Then symbol = "$$"
    
    ' Define regular expression pattern and replacement string.
    Dim pattern As String = "\b(\d+)(" + cSeparator + "(\d+))?" 
    Dim replacement As String = "$1$2"
    replacement = If(precedes, symbol + " " + replacement, replacement + " " + symbol)
    For Each value In values
     Console.WriteLine("{0} --> {1}", value, Regex.Replace(value, pattern, replacement))
    Next
   End Sub
End Module
' The example displays the following output:
'     16.35 --> $ 16.35
'     19.72 --> $ 19.72
'     1234 --> $ 1234
'     0.99 --> $ 0.99

正则表达式模式 \b(\d+)(\.(\d+))? 的定义如下表所示。

模式说明
\b从字边界开始进行匹配。
(\d+)匹配一个或多个十进制数字。 这是第一个捕获组。
\.与句点(小数点分隔符)匹配。
(\d+)匹配一个或多个十进制数字。 这是第三个捕获组。
(\.(\d+))?与零个或一个后跟一个或多个十进制数字的句点匹配。 这是第二个捕获组。

替换整个匹配项

$& 替换包括替换字符串中的整个匹配项。 通常,它用于将子字符串添加至匹配字符串的开头或末尾。 例如, ($&) 替换模式向每个匹配项的开头和结尾添加括号。 如果没有匹配项,则 $& 替换将不起作用。

下面的示例使用 $& 替换在存储于字符串数组中的书名的开头和结尾添加引号。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    string pattern = @"^(\w+\s?)+$";
    string[] titles = { "A Tale of Two Cities", 
              "The Hound of the Baskervilles", 
              "The Protestant Ethic and the Spirit of Capitalism", 
              "The Origin of Species" };
    string replacement = "\"$&\"";
    foreach (string title in titles)
     Console.WriteLine(Regex.Replace(title, pattern, replacement));
   }
}
// The example displays the following output:
//     "A Tale of Two Cities"
//     "The Hound of the Baskervilles"
//     "The Protestant Ethic and the Spirit of Capitalism"
//     "The Origin of Species"
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    Dim pattern As String = "^(\w+\s?)+$"
    Dim titles() As String = { "A Tale of Two Cities", _
                 "The Hound of the Baskervilles", _
                 "The Protestant Ethic and the Spirit of Capitalism", _
                 "The Origin of Species" }
    Dim replacement As String = """$&"""
    For Each title As String In titles
     Console.WriteLine(Regex.Replace(title, pattern, replacement))
    Next  
   End Sub
End Module
' The example displays the following output:
'     "A Tale of Two Cities"
'     "The Hound of the Baskervilles"
'     "The Protestant Ethic and the Spirit of Capitalism"
'     "The Origin of Species"

正则表达式模式 ^(\w+\s?)+$ 的定义如下表所示。

模式说明
^从输入字符串的开头部分开始匹配。
(\w+\s?)+匹配一个或多个单词字符后跟零个或一个空白字符的模式一次或多次。
$匹配输入字符串的末尾部分。

"$&" 替换模式将文本引号添加到每个匹配项的开头和结尾。

返回页首

替换匹配项前的文本

$\ </code> 替换将匹配的字符串替换为匹配项前面的整个输入字符串。</span><span class="sxs-lookup"><span data-stu-id="e2cd6-215">The <code>$\\ substitution replaces the matched string with the entire input string before the match. 即,它将在删除匹配的文本时重复输入字符串,直至匹配。 匹配文本后面的任何文本在结果字符串中保持不变。 如果输入字符串中有多个匹配项,则替换文本将派生自原始输入字符串,而不是派生自文本已由早期匹配项替换的字符串。 (示例进行了说明。) 如果没有匹配项,则 $\ </code> 替换将不起作用。</span><span class="sxs-lookup"><span data-stu-id="e2cd6-219">\(The example provides an illustration.\) If there is no match, the <code>$\\ substitution has no effect.

下面的示例使用正则表达式模式 \d+ 来匹配输入字符串中一个或多个十进制数字的序列。 替换字符串 $` 将这些数字替换为该匹配项前的文本。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    string input = "aa1bb2cc3dd4ee5";
    string pattern = @"\d+";
    string substitution = "$`";
    Console.WriteLine("Matches:");
    foreach (Match match in Regex.Matches(input, pattern))
     Console.WriteLine("   {0} at position {1}", match.Value, match.Index);

    Console.WriteLine("Input string:  {0}", input);
    Console.WriteLine("Output string: " + 
            Regex.Replace(input, pattern, substitution));
   }
}
// The example displays the following output:
//  Matches:
//     1 at position 2
//     2 at position 5
//     3 at position 8
//     4 at position 11
//     5 at position 14
//  Input string:  aa1bb2cc3dd4ee5
//  Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    Dim input As String = "aa1bb2cc3dd4ee5"
    Dim pattern As String = "\d+"
    Dim substitution As String = "$`"
    Console.WriteLine("Matches:")
    For Each match As Match In Regex.Matches(input, pattern)
     Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
    Next   
    Console.WriteLine("Input string:  {0}", input)
    Console.WriteLine("Output string: " + _
            Regex.Replace(input, pattern, substitution))
   End Sub
End Module
' The example displays the following output:
'  Matches:
'     1 at position 2
'     2 at position 5
'     3 at position 8
'     4 at position 11
'     5 at position 14
'  Input string:  aa1bb2cc3dd4ee5
'  Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee

在此示例中,输入字符串 "aa1bb2cc3dd4ee5" 包含五个匹配项。 下表说明了 $` 替换如何使正则表达式引擎替换输入字符串中的每个匹配项。 插入文本在结果列中以粗体显示。

匹配位置匹配项前的字符串结果字符串
12aaaa aa bb2cc3dd4ee5
25aa1bbaaaabb aa1bb cc3dd4ee5
38aa1bb2ccaaaabbaa1bbcc aa1bb2cc dd4ee5
411aa1bb2cc3ddaaaabbaa1bbccaa1bb2ccdd aa1bb2cc3dd ee5
514aa1bb2cc3dd4eeaaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddee aa1bb2cc3dd4ee

返回页首

替换匹配项后的文本

$' 替换将匹配的字符串替换为匹配项后的整个输入字符串。 即,它将在删除匹配的文本时重复匹配项后的输入字符串。 匹配文本前面的任何文本在结果字符串中保持不变。 如果没有匹配项,则 $' 替换将不起作用。

下面的示例使用正则表达式模式 \d+ 来匹配输入字符串中一个或多个十进制数字的序列。 替换字符串 $' 将这些数字替换为匹配项后的文本。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    string input = "aa1bb2cc3dd4ee5";
    string pattern = @"\d+";
    string substitution = "$'";
    Console.WriteLine("Matches:");
    foreach (Match match in Regex.Matches(input, pattern))
     Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
    Console.WriteLine("Input string:  {0}", input);
    Console.WriteLine("Output string: " + 
            Regex.Replace(input, pattern, substitution));
   }
}
// The example displays the following output:
//  Matches:
//     1 at position 2
//     2 at position 5
//     3 at position 8
//     4 at position 11
//     5 at position 14
//  Input string:  aa1bb2cc3dd4ee5
//  Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    Dim input As String = "aa1bb2cc3dd4ee5"
    Dim pattern As String = "\d+"
    Dim substitution As String = "$'"
    Console.WriteLine("Matches:")
    For Each match As Match In Regex.Matches(input, pattern)
     Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
    Next   
    Console.WriteLine("Input string:  {0}", input)
    Console.WriteLine("Output string: " + _
            Regex.Replace(input, pattern, substitution))
   End Sub
End Module
' The example displays the following output:
'  Matches:
'     1 at position 2
'     2 at position 5
'     3 at position 8
'     4 at position 11
'     5 at position 14
'  Input string:  aa1bb2cc3dd4ee5
'  Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee

在此示例中,输入字符串 "aa1bb2cc3dd4ee5" 包含五个匹配项。 下表说明了 $' 替换如何使正则表达式引擎替换输入字符串中的每个匹配项。 插入文本在结果列中以粗体显示。

匹配位置匹配项后的字符串结果字符串
12bb2cc3dd4ee5aa bb2cc3dd4ee5 bb2cc3dd4ee5
25cc3dd4ee5aabb2cc3dd4ee5bb cc3dd4ee5 cc3dd4ee5
38dd4ee5aabb2cc3dd4ee5bbcc3dd4ee5cc dd4ee5 dd4ee5
411ee5aabb2cc3dd4ee5bbcc3dd4ee5ccdd4ee5dd ee5 ee5
514String.Emptyaabb2cc3dd4ee5bbcc3dd4ee5ccdd4ee5ddee5ee

返回页首

替换最后捕获的组

$+ 替换将匹配的字符串替换为最后捕获的组。 如果没有捕获组,或者最后的捕获组的值是 String.Empty ,则 $+ 替换将不起作用。

下面的示例标识字符串中的重复单词,并使用 $+ 替换将其替换为该单词的单一匹配项。 RegexOptions.IgnoreCase 选项用于确保大小写不同但其他内容都相同的单词被认为是重复的。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    string pattern = @"\b(\w+)\s\1\b";
    string substitution = "$+";
    string input = "The the dog jumped over the fence fence.";
    Console.WriteLine(Regex.Replace(input, pattern, substitution, 
            RegexOptions.IgnoreCase));
   }
}
// The example displays the following output:
//    The dog jumped over the fence.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    Dim pattern As String = "\b(\w+)\s\1\b"
    Dim substitution As String = "$+"
    Dim input As String = "The the dog jumped over the fence fence."
    Console.WriteLine(Regex.Replace(input, pattern, substitution, _
                    RegexOptions.IgnoreCase))
   End Sub
End Module
' The example displays the following output:
'    The dog jumped over the fence.

正则表达式模式 \b(\w+)\s\1\b 的定义如下表所示。

模式描述
\b在单词边界处开始匹配。
(\w+)匹配一个或多个单词字符。 这是第一个捕获组。
\s与空白字符匹配。
\1与第一个捕获的组匹配。
\b在单词边界处结束匹配。

返回页首

替换整个输入字符串

$_ 替换将匹配的字符串替换为整个输入字符。 即,它将删除匹配的文本并将其替换为整个字符串(包括匹配的文本)。

下面的示例匹配输入字符串中的一个或多个十进制数字。 它使用 $_ 替换来将其替换为整个输入字符串。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
    string input = "ABC123DEF456";
    string pattern = @"\d+";
    string substitution = "$_";
    Console.WriteLine("Original string:      {0}", input);
    Console.WriteLine("String with substitution: {0}", 
            Regex.Replace(input, pattern, substitution));    
   }
}
// The example displays the following output:
//     Original string:      ABC123DEF456
//     String with substitution: ABCABC123DEF456DEFABC123DEF456
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
    Dim input As String = "ABC123DEF456"
    Dim pattern As String = "\d+"
    Dim substitution As String = "$_"
    Console.WriteLine("Original string:      {0}", input)
    Console.WriteLine("String with substitution: {0}", _
            Regex.Replace(input, pattern, substitution))    
   End Sub
End Module
' The example displays the following output:
'     Original string:      ABC123DEF456
'     String with substitution: ABCABC123DEF456DEFABC123DEF456

在此示例中,输入字符串 "ABC123DEF456" 包含两个匹配项。 下表说明了 $_ 替换如何使正则表达式引擎替换输入字符串中的每个匹配项。 插入文本在结果列中以粗体显示。

匹配位置匹配结果字符串
13123ABC ABC123DEF456 DEF456
25456ABCABC123DEF456DEF ABC123DEF456

另请参阅

正则表达式语言 - 快速参考

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文