。
后接字符未识别为转义字符时,将匹配此字符。 例如,
\*
匹配星号 (*) 并等同于
\x2A
。
以下示例说明了如何使用正则表达式中的字符转义。 分析了包含 2009 年世界上最大城市的名称及其人口的字符串。 使用制表符 (
\t
) 或垂直条(| 或
\u007c
)将每个城市名与其人口数量分开。 使用回车符和换行符分隔各个城市及其人口。
using System;
using System.Text.RegularExpressions;
public class Example
public static void Main()
string delimited = @"\G(.+)[\t\u007c](.+)\r?\n";
string input = "Mumbai, India|13,922,125\t\n" +
"Shanghai, China\t13,831,900\n" +
"Karachi, Pakistan|12,991,000\n" +
"Delhi, India\t12,259,230\n" +
"Istanbul, Turkey|11,372,613\n";
Console.WriteLine("Population of the World's Largest Cities, 2009");
Console.WriteLine();
Console.WriteLine("{0,-20} {1,10}", "City", "Population");
Console.WriteLine();
foreach (Match match in Regex.Matches(input, delimited))
Console.WriteLine("{0,-20} {1,10}", match.Groups[1].Value,
match.Groups[2].Value);
// The example displays the following output:
// Population of the World's Largest Cities, 2009
// City Population
// Mumbai, India 13,922,125
// Shanghai, China 13,831,900
// Karachi, Pakistan 12,991,000
// Delhi, India 12,259,230
// Istanbul, Turkey 11,372,613
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim delimited As String = "\G(.+)[\t\u007c](.+)\r?\n"
Dim input As String = "Mumbai, India|13,922,125" + vbCrLf + _
"Shanghai, China" + vbTab + "13,831,900" + vbCrLf + _
"Karachi, Pakistan|12,991,000" + vbCrLf + _
"Delhi, India" + vbTab + "12,259,230" + vbCrLf + _
"Istanbul, Turkey|11,372,613" + vbCrLf
Console.WriteLine("Population of the World's Largest Cities, 2009")
Console.WriteLine()
Console.WriteLine("{0,-20} {1,10}", "City", "Population")
Console.WriteLine()
For Each match As Match In Regex.Matches(input, delimited)
Console.WriteLine("{0,-20} {1,10}", match.Groups(1).Value, _
match.Groups(2).Value)
End Sub
End Module
' The example displays the following output:
' Population of the World's Largest Cities, 2009
' City Population
' Mumbai, India 13,922,125
' Shanghai, China 13,831,900
' Karachi, Pakistan 12,991,000
' Delhi, India 12,259,230
' Istanbul, Turkey 11,372,613
正则表达式 \G(.+)[\t\u007c](.+)\r?\n 可以解释为下表中所示内容。