替代
替换是只能在替换模式中识别的语言元素。 它们使用正则表达式模式定义全部或部分用于替换输入字符串中的匹配文本的文本。 替换模式可以包含一个或多个替换以及本文字符。 提供替换模式以将拥有 replacement 参数的 Regex.Replace 方法重载至 Match.Result 方法。 该方法将匹配的模式替换为 replacement 参数定义的模式。
.NET Framework 定义下表列出的替换元素。
替换 |
说明 |
---|---|
$数值 |
包括替换字符串中的由 number 定义的捕获组所匹配的最后一个子字符串,number 是一个十进制值。 有关更多信息,请参见替换带编号的组。 |
${name} |
包括替换字符串中由 (?<name> ) 指定的命名组所匹配的最后一个子字符串。 有关更多信息,请参见替换命名组。 |
$$ |
包括替换字符串中的单个“$”文本。 有关更多信息,请参见替换“$”符号。 |
$& |
包括替换字符串中整个匹配的副本。 有关更多信息,请参见替换整个匹配。 |
$` |
包括替换字符串中的匹配前的输入字符串的所有文本。 有关更多信息,请参见替换匹配前的文本。 |
$' |
包括替换字符串中的匹配后的输入字符串的所有文本。 有关更多信息,请参见替换匹配后的文本。 |
$+ |
包括在替换字符串中捕获的最后一个组。 有关更多信息,请参见替换最后捕获的组。 |
$_ |
包括替换字符串中的整个输入字符串。 有关更多信息,请参见替换整个输入字符串。 |
替换元素和替换模式
替换是在替换模式中可唯一识别的特殊构造。 与任何字符匹配的其他正则表达式语言元素(包括字符转义和句点 (.))均不受支持。 同样,替换语言元素只能在替换模式中识别,并且在正则表达式模式中永远无效。
可以出现在正则表达式模式或替换中的唯一字符是 $ 字符,尽管它在每个上下文中具有不同的含义。 在正则表达式模式中,$ 是与字符串的末尾匹配的定位点。 在替换模式中,$ 指示替换的开头。
注意 |
---|
对于类似于正则表达式中替换模式的功能,使用反向引用。有关反向引用的更多信息,请参见反向引用构造。 |
替换带编号的组
$数字 语言元素包括 数字 捕获组匹配的最后一个子字符串,其中 数字 是捕获组的索引。 例如,替换模式 $1 指示匹配的字符串替换为第一个被捕获组。 有关为捕获组编号的详细信息,请参见分组构造。
没有使用 (?< 名称 >) 语法显式分配的名称的捕获组从左到右编号,从1开始。 命名组还从左到右编号,从最后一个未命名的组的索引大于 1 开始。 例如,在正则表达式 (\w)(?<digit>\d) 中,digit 命名组的索引为 2。
如果 数字 未指定在正则表达式模式中定义的有效的捕获组,$number 被解释为原义字符序列,用于替换的每个匹配项。
下面的示例使用 $数字 替换去除十进制值中的货币符号。 它移除在货币值的开头或末尾找到的货币符号,并识别到两个最常见的小数点分隔符("." 和 ",")。
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
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
正则表达式模式 \p{Sc}*(\s?\d+[.,]?\d*)\p{Sc}* 的定义如下表所示。
模式 |
说明 |
---|---|
\p{Sc}* |
与零个或多个货币符号字符匹配。 |
\s? |
匹配零或一个空格字符。 |
\d+ |
匹配一个或多个十进制数字。 |
[.,]? |
与零个或一个句点或逗号字符匹配。 |
\d* |
匹配零个或多个十进制数字。 |
(\s? \d+[.,]? \d*) |
匹配逗号的零个或一个匹配项,后跟可选负号,负号后跟一个或多个十进制数。 这是第一个捕获组。 因为替换模式为 $1,所以对 Regex.Replace 方法的调用会将整个匹配的子字符串替换为此捕获组。 |
返回页首
替换命名组
${名称} 语言元素替换 名称 捕获组匹配的最后一个子字符串,其中 名称 是 (?<名称>) 语言元素所定义的捕获组的名称。 有关为捕获组命名的详细信息,请参见分组构造。
如果 名称 未指定在正则表达式模式中定义的有效的已命名的捕获组,${名称} 被解释为原义字符序列,用于替换的每个匹配项。
下面的示例使用 ${名称} 替换去除十进制值中的货币符号。 它移除在货币值的开头或末尾找到的货币符号,并识别到两个最常见的小数点分隔符("." 和 ",")。
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
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
正则表达式模式 \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。 替换模式将匹配的文本替换为一个后跟第一个和第二个捕获组的货币符号和空格。
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
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
正则表达式模式 \b(\d+)(\.(\d+))? 的定义如下表所示。
模式 |
说明 |
---|---|
\b |
从单词边界开始进行匹配。 |
(\d+) |
匹配一个或多个十进制数字。 这是第一个捕获组。 |
\. |
与句号(小数点分隔符)匹配。 |
(\d+) |
匹配一个或多个十进制数字。 这是第三个捕获组。 |
(\.(\d+))? |
与零个或一个后接一个或多个十进制数字的句点匹配。 这是第二个捕获组。 |
替换整个匹配
$& 替换包括替换字符串中的整个匹配。 通常,它用于将子字符串添加至匹配字符串的开头或末尾。 例如,($&) 替换模式向每个匹配项的开头和结尾添加括号。 如果不匹配,则 $& 替换将不起作用。
下面的示例使用 $& 替换在存储于字符串数组中的书名的开头和结尾添加引号。
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"
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"
正则表达式模式 ^(\w+\s?)+$ 的定义如下表所示。
模式 |
说明 |
---|---|
^ |
从输入字符串的开头部分开始匹配。 |
(\w+\s?)+ |
一次或多次匹配一个或多个单词字符的模式,这些希腊字符后跟零或一个空白字符。 |
$ |
匹配输入字符串的末尾部分。 |
"$&" 替换模式将文本引号添加到各匹配的开头和结尾。
返回页首
替换匹配前的文本
$` 替换将匹配的字符串替换为匹配前的整个输入字符串。 即,它将在删除匹配的文本时重复输入字符串,直至匹配。 遵循匹配文本的任何文本在结果字符串中是不变的。 如果在输入字符串中有多个匹配项,则替换文本将派生自原始输入字符串,而非派生自由早期匹配项替换文本的字符串。 (该示例提供了说明。)如果不匹配,则 $` 替换将不起作用。
下面的示例使用正则表达式模式 \d+ 来匹配输入字符串中一个或多个十进制数字的序列。 替换字符串 $` 将这些数字替换为该匹配前的文本。
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
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
在此示例中,输入字符串 "aa1bb2cc3dd4ee5" 包含五个匹配项。 下表说明了 $` 替换如何使正则表达式引擎替换输入字符串中的每个匹配项。 插入文本在结果列中以粗体显示。
Match |
Position |
匹配前的字符串 |
结果字符串 |
---|---|---|---|
1 |
2 |
aa |
aa aa bb2cc3dd4ee5 |
2 |
5 |
aa1bb |
aaaabb aa1bb cc3dd4ee5 |
3 |
8 |
aa1bb2cc |
aaaabbaa1bbcc aa1bb2cc dd4ee5 |
4 |
11 |
aa1bb2cc3dd |
aaaabbaa1bbccaa1bb2ccdd aa1bb2cc3dd ee5 |
5 |
14 |
aa1bb2cc3dd4ee |
aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddee aa1bb2cc3dd4ee |
返回页首
替换匹配后的文本
$' 替换将匹配的字符串替换为匹配后的整个输入字符串。 即,它将在删除匹配的文本时重复匹配后的输入字符串。 位于匹配文本之前的任何文本在结果字符串中是不变的。 如果没有匹配项,则 $' 替换将不起作用。
下面的示例使用正则表达式模式 \d+ 来匹配输入字符串中一个或多个十进制数字的序列。 替换字符串 $' 将这些数字替换为该匹配后的文本。
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
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
在此示例中,输入字符串 "aa1bb2cc3dd4ee5" 包含五个匹配项。 下表说明了 $' 替换如何使正则表达式引擎替换输入字符串中的每个匹配项。 插入文本在结果列中以粗体显示。
Match |
Position |
匹配后的字符串 |
结果字符串 |
---|---|---|---|
1 |
2 |
bb2cc3dd4ee5 |
aa bb2cc3dd4ee5 bb2cc3dd4ee5 |
2 |
5 |
cc3dd4ee5 |
aabb2cc3dd4ee5bb cc3dd4ee5 cc3dd4ee5 |
3 |
8 |
dd4ee5 |
aabb2cc3dd4ee5bbcc3dd4ee5cc dd4ee5 dd4ee5 |
4 |
11 |
ee5 |
aabb2cc3dd4ee5bbcc3dd4ee5ccdd4ee5dd ee5 ee5 |
5 |
14 |
aabb2cc3dd4ee5bbcc3dd4ee5ccdd4ee5ddee5ee |
返回页首
替换最后捕获的组
$+ 替换将匹配的字符串替换为最后捕获的组。 如果没有捕获组,或者最后的捕获组的值是 String.Empty,则 $+ 替换将不起作用。
下面的示例标识字符串中的重复单词,并使用 $+ 替换将其替换为该单词的单一匹配项。 RegexOptions.IgnoreCase 选项用于确保大小写不同但其他内容都相同的单词被认为是重复的。
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.
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.
正则表达式模式 \b(\w+)\s\1\b 的定义如下表所示。
模式 |
说明 |
---|---|
\b |
在单词边界处开始匹配。 |
(\w+) |
匹配一个或多个单词字符。 这是第一个捕获组。 |
\s |
与空白字符匹配。 |
\1 |
与第一个被捕获组匹配。 |
\b |
在单词边界处结束匹配。 |
返回页首
替换整个输入字符串。
$_ 替换将匹配的字符串替换为整个输入字符。 即,它将删除匹配的文本并将其替换为整个字符串(包括匹配的文本)。
下面的示例匹配输入字符串中的一个或多个十进制数字。 它使用 $_ 替换来以整个输入字符串替换它们。
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
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
在此示例中,输入字符串 "ABC123DEF456" 包含两个匹配项。 下表说明了 $_ 替换如何使正则表达式引擎替换输入字符串中的每个匹配项。 插入文本在结果列中以粗体显示。
Match |
Position |
Match |
结果字符串 |
---|---|---|---|
1 |
3 |
123 |
ABC ABC123DEF456 DEF456 |
2 |
5 |
456 |
ABCABC123DEF456DEF ABC123DEF456 |
返回页首