正则表达式中的定位点Anchors in Regular Expressions

定位点(原子零宽度断言)指定字符串中必须出现匹配的位置。Anchors, or atomic zero-width assertions, specify a position in the string where a match must occur. 在搜索表达式中使用定位点时,正则表达式引擎不在字符串中前进或使用字符,它仅在指定位置查找匹配。When you use an anchor in your search expression, the regular expression engine does not advance through the string or consume characters; it looks for a match in the specified position only. 例如, ^ 指定必须从行或字符串的开头开始匹配。For example, ^ specifies that the match must start at the beginning of a line or string. 因此,正则表达式 ^http: 仅当 "http:" 出现在行开头时才与之匹配。Therefore, the regular expression ^http: matches "http:" only when it occurs at the beginning of a line. 下表列出了 .NET 中正则表达式支持的定位点。The following table lists the anchors supported by the regular expressions in .NET.

定位点Anchor 描述Description
^ 默认情况下,匹配必须出现在字符串的开头;在多行模式中,必须出现在该行的开头。By default, the match must occur at the beginning of the string; in multiline mode, it must occur at the beginning of the line. 有关详细信息,请参阅 字符串或行的开头For more information, see Start of String or Line.
$ 默认情况下,匹配必须出现在字符串的末尾,或在字符串末尾的 \n 之前;在多行模式中,必须出现在该行的末尾,或在该行末尾的 \n 之前。By default, the match must occur at the end of the string or before \n at the end of the string; in multiline mode, it must occur at the end of the line or before \n at the end of the line. 有关详细信息,请参阅 字符串或行的末尾For more information, see End of String or Line.
\A 匹配必须仅出现在字符串的开头位置(无多行支持)。The match must occur at the beginning of the string only (no multiline support). 有关详细信息,请参阅 仅字符串的开头For more information, see Start of String Only.
\Z 匹配必须出现在字符串的末尾,或出现在字符串末尾的 \n 之前。The match must occur at the end of the string, or before \n at the end of the string. 有关详细信息,请参阅 字符串的末尾或结束换行之前For more information, see End of String or Before Ending Newline.
\z 匹配必须仅出现在字符串的末尾。The match must occur at the end of the string only. 有关详细信息,请参阅 仅字符串的末尾For more information, see End of String Only.
\G 匹配必须从上一个匹配结束的位置开始。The match must start at the position where the previous match ended. 有关详细信息,请参阅 连续匹配For more information, see Contiguous Matches.
\b 匹配必须出现在字边界。The match must occur on a word boundary. 有关详细信息,请参阅 字边界For more information, see Word Boundary.
\B 匹配不得出现在字边界上。The match must not occur on a word boundary. 有关详细信息,请参阅 非字边界For more information, see Non-Word Boundary.

字符串或行的开头:^Start of String or Line: ^

默认情况下,^ 定位点指定以下模式必须从字符串的第一个字符位置开始。By default, the ^ anchor specifies that the following pattern must begin at the first character position of the string. 如果结合使用 ^RegexOptions.Multiline 选项(请参阅正则表达式选项),匹配必须出现在每行的开头。If you use ^ with the RegexOptions.Multiline option (see Regular Expression Options), the match must occur at the beginning of each line.

以下示例在正则表达式中使用 ^ 定位点,可提取有关某些职业棒球队存在年限的信息。The following example uses the ^ anchor in a regular expression that extracts information about the years during which some professional baseball teams existed. 该示例调用 Regex.Matches 方法的两个重载:The example calls two overloads of the Regex.Matches method:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string input = "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957\n" +
                     "Chicago Cubs, National League, 1903-present\n" +
                     "Detroit Tigers, American League, 1901-present\n" +
                     "New York Giants, National League, 1885-1957\n" +
                     "Washington Senators, American League, 1901-1960\n";
        string pattern = @"^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+";
        Match match;

        match = Regex.Match(input, pattern);
        while (match.Success)
        {
            Console.Write("The {0} played in the {1} in",
                              match.Groups[1].Value, match.Groups[4].Value);
            foreach (Capture capture in match.Groups[5].Captures)
                Console.Write(capture.Value);

            Console.WriteLine(".");
            match = match.NextMatch();
        }
        Console.WriteLine();

        match = Regex.Match(input, pattern, RegexOptions.Multiline);
        while (match.Success)
        {
            Console.Write("The {0} played in the {1} in",
                              match.Groups[1].Value, match.Groups[4].Value);
            foreach (Capture capture in match.Groups[5].Captures)
                Console.Write(capture.Value);

            Console.WriteLine(".");
            match = match.NextMatch();
        }
        Console.WriteLine();
    }
}
// The example displays the following output:
//    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
//
//    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
//    The Chicago Cubs played in the National League in 1903-present.
//    The Detroit Tigers played in the American League in 1901-present.
//    The New York Giants played in the National League in 1885-1957.
//    The Washington Senators played in the American League in 1901-1960.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957" + vbCrLf +
                              "Chicago Cubs, National League, 1903-present" + vbCrLf +
                              "Detroit Tigers, American League, 1901-present" + vbCrLf +
                              "New York Giants, National League, 1885-1957" + vbCrLf +
                              "Washington Senators, American League, 1901-1960" + vbCrLf

        Dim pattern As String = "^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+"
        Dim match As Match

        match = Regex.Match(input, pattern)
        Do While match.Success
            Console.Write("The {0} played in the {1} in",
                              match.Groups(1).Value, match.Groups(4).Value)
            For Each capture As Capture In match.Groups(5).Captures
                Console.Write(capture.Value)
            Next
            Console.WriteLine(".")
            match = match.NextMatch()
        Loop
        Console.WriteLine()

        match = Regex.Match(input, pattern, RegexOptions.Multiline)
        Do While match.Success
            Console.Write("The {0} played in the {1} in",
                              match.Groups(1).Value, match.Groups(4).Value)
            For Each capture As Capture In match.Groups(5).Captures
                Console.Write(capture.Value)
            Next
            Console.WriteLine(".")
            match = match.NextMatch()
        Loop
        Console.WriteLine()
    End Sub
End Module
' The example displays the following output:
'    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
'    
'    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
'    The Chicago Cubs played in the National League in 1903-present.
'    The Detroit Tigers played in the American League in 1901-present.
'    The New York Giants played in the National League in 1885-1957.
'    The Washington Senators played in the American League in 1901-1960.

正则表达式模式 ^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+ 的定义如下表所示。The regular expression pattern ^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+ is defined as shown in the following table.

模式Pattern 描述Description
^ 从输入字符串的开头开始匹配(如果在调用该方法时选择了 RegexOptions.Multiline 选项,则从行的开头开始匹配)。Begin the match at the beginning of the input string (or the beginning of the line if the method is called with the RegexOptions.Multiline option).
((\w+(\s?)){2,} 匹配至少两次后跟零个或一个空格的一个或多个单词字符。Match one or more word characters followed either by zero or by one space at least two times. 这是第一个捕获组。This is the first capturing group. 此表达式还定义第二个和第三个捕获组:第二个捕获组由捕获的单词组成,第三个捕获组由捕获的空格组成。This expression also defines a second and third capturing group: The second consists of the captured word, and the third consists of the captured white space.
,\s 匹配后跟一个空白字符的逗号。Match a comma followed by a white-space character.
(\w+\s\w+) 匹配后跟一个空格再后跟一个或多个单词字符的一个或多个单词字符。Match one or more word characters followed by a space, followed by one or more word characters. 这是第四个捕获组。This is the fourth capturing group.
, 匹配逗号。Match a comma.
\s\d{4} 匹配后跟四个十进制数字的空格。Match a space followed by four decimal digits.
(-(\d{4}|present))? 匹配连字符后跟四个十进制数字或字符串“present”的零或一个匹配项。Match zero or one occurrence of a hyphen followed by four decimal digits or the string "present". 这是第六个捕获组。This is the sixth capturing group. 还包括第七个捕获组。It also includes a seventh capturing group.
,? 匹配逗号的零个或一个匹配项。Match zero or one occurrence of a comma.
(\s\d{4}(-(\d{4}|present))?,?)+ 匹配以下内容的一个或多个匹配项:空格、四个十进制数字、连字符后跟四个十进制数字或字符串“present”的零个或一个匹配项以及零个或一个逗号。Match one or more occurrences of the following: a space, four decimal digits, zero or one occurrence of a hyphen followed by four decimal digits or the string "present", and zero or one comma. 这是第五个捕获组。This is the fifth capturing group.

字符串或行的末尾:$End of String or Line: $

$ 定位点指定前面的模式必须出现在输入字符串的末尾,或出现在输入字符串末尾的 \n 之前。The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string.

如果结合使用 $RegexOptions.Multiline 选项,则匹配也可能出现在行的末尾。If you use $ with the RegexOptions.Multiline option, the match can also occur at the end of a line. 请注意, $\n 匹配但与 \r\n (回车符和换行符的组合,或称 CR/LF)不匹配。Note that $ matches \n but does not match \r\n (the combination of carriage return and newline characters, or CR/LF). 若要匹配 CR/LF 字符组合,请将 \r?$ 包括到正则表达式模式中。To match the CR/LF character combination, include \r?$ in the regular expression pattern.

以下示例将 $ 定位点添加到 字符串或行的开头 部分的示例中所使用的正则表达式模式中。The following example adds the $ anchor to the regular expression pattern used in the example in the Start of String or Line section. 配合包括五行文本的原始输入字符串使用时, Regex.Matches(String, String) 方法找不到匹配项,因为第一行的末尾与 $ 模式不匹配。When used with the original input string, which includes five lines of text, the Regex.Matches(String, String) method is unable to find a match, because the end of the first line does not match the $ pattern. 当原始输入字符串被拆分为字符串数组时, Regex.Matches(String, String) 方法会成功匹配五行中的每一行。When the original input string is split into a string array, the Regex.Matches(String, String) method succeeds in matching each of the five lines. 如果调用 Regex.Matches(String, String, RegexOptions) 方法时将 options 参数设置为 RegexOptions.Multiline,则找不到匹配项,因为正则表达式模式不考虑回车符元素 (\u+000D)。When the Regex.Matches(String, String, RegexOptions) method is called with the options parameter set to RegexOptions.Multiline, no matches are found because the regular expression pattern does not account for the carriage return element (\u+000D). 但是,如果通过用将 $ 替换成 \r?$修改了正则表达式模式,则调用 Regex.Matches(String, String, RegexOptions) 方法并将 options 参数设置为 RegexOptions.Multiline 将再次找到五个匹配项。However, when the regular expression pattern is modified by replacing $ with \r?$, calling the Regex.Matches(String, String, RegexOptions) method with the options parameter set to RegexOptions.Multiline again finds five matches.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string cr = Environment.NewLine;
        string input = "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957" + cr +
                       "Chicago Cubs, National League, 1903-present" + cr +
                       "Detroit Tigers, American League, 1901-present" + cr +
                       "New York Giants, National League, 1885-1957" + cr +
                       "Washington Senators, American League, 1901-1960" + cr;
        Match match;

        string basePattern = @"^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+";
        string pattern = basePattern + "$";
        Console.WriteLine("Attempting to match the entire input string:");
        match = Regex.Match(input, pattern);
        while (match.Success)
        {
            Console.Write("The {0} played in the {1} in",
                              match.Groups[1].Value, match.Groups[4].Value);
            foreach (Capture capture in match.Groups[5].Captures)
                Console.Write(capture.Value);

            Console.WriteLine(".");
            match = match.NextMatch();
        }
        Console.WriteLine();

        string[] teams = input.Split(new String[] { cr }, StringSplitOptions.RemoveEmptyEntries);
        Console.WriteLine("Attempting to match each element in a string array:");
        foreach (string team in teams)
        {
            match = Regex.Match(team, pattern);
            if (match.Success)
            {
                Console.Write("The {0} played in the {1} in",
                              match.Groups[1].Value, match.Groups[4].Value);
                foreach (Capture capture in match.Groups[5].Captures)
                    Console.Write(capture.Value);
                Console.WriteLine(".");
            }
        }
        Console.WriteLine();

        Console.WriteLine("Attempting to match each line of an input string with '$':");
        match = Regex.Match(input, pattern, RegexOptions.Multiline);
        while (match.Success)
        {
            Console.Write("The {0} played in the {1} in",
                              match.Groups[1].Value, match.Groups[4].Value);
            foreach (Capture capture in match.Groups[5].Captures)
                Console.Write(capture.Value);

            Console.WriteLine(".");
            match = match.NextMatch();
        }
        Console.WriteLine();

        pattern = basePattern + "\r?$";
        Console.WriteLine(@"Attempting to match each line of an input string with '\r?$':");
        match = Regex.Match(input, pattern, RegexOptions.Multiline);
        while (match.Success)
        {
            Console.Write("The {0} played in the {1} in",
                              match.Groups[1].Value, match.Groups[4].Value);
            foreach (Capture capture in match.Groups[5].Captures)
                Console.Write(capture.Value);

            Console.WriteLine(".");
            match = match.NextMatch();
        }
        Console.WriteLine();
    }
}
// The example displays the following output:
//    Attempting to match the entire input string:
//
//    Attempting to match each element in a string array:
//    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
//    The Chicago Cubs played in the National League in 1903-present.
//    The Detroit Tigers played in the American League in 1901-present.
//    The New York Giants played in the National League in 1885-1957.
//    The Washington Senators played in the American League in 1901-1960.
//
//    Attempting to match each line of an input string with '$':
//
//    Attempting to match each line of an input string with '\r?$':
//    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
//    The Chicago Cubs played in the National League in 1903-present.
//    The Detroit Tigers played in the American League in 1901-present.
//    The New York Giants played in the National League in 1885-1957.
//    The Washington Senators played in the American League in 1901-1960.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957" + vbCrLf +
                              "Chicago Cubs, National League, 1903-present" + vbCrLf +
                              "Detroit Tigers, American League, 1901-present" + vbCrLf +
                              "New York Giants, National League, 1885-1957" + vbCrLf +
                              "Washington Senators, American League, 1901-1960" + vbCrLf

        Dim basePattern As String = "^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+"
        Dim match As Match

        Dim pattern As String = basePattern + "$"
        Console.WriteLine("Attempting to match the entire input string:")
        match = Regex.Match(input, pattern)
        Do While match.Success
            Console.Write("The {0} played in the {1} in",
                              match.Groups(1).Value, match.Groups(4).Value)
            For Each capture As Capture In match.Groups(5).Captures
                Console.Write(capture.Value)
            Next
            Console.WriteLine(".")
            match = match.NextMatch()
        Loop
        Console.WriteLine()

        Dim teams() As String = input.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
        Console.WriteLine("Attempting to match each element in a string array:")
        For Each team As String In teams
            match = Regex.Match(team, pattern)
            If match.Success Then
                Console.Write("The {0} played in the {1} in",
                               match.Groups(1).Value, match.Groups(4).Value)
                For Each capture As Capture In match.Groups(5).Captures
                    Console.Write(capture.Value)
                Next
                Console.WriteLine(".")
            End If
        Next
        Console.WriteLine()

        Console.WriteLine("Attempting to match each line of an input string with '$':")
        match = Regex.Match(input, pattern, RegexOptions.Multiline)
        Do While match.Success
            Console.Write("The {0} played in the {1} in",
                              match.Groups(1).Value, match.Groups(4).Value)
            For Each capture As Capture In match.Groups(5).Captures
                Console.Write(capture.Value)
            Next
            Console.WriteLine(".")
            match = match.NextMatch()
        Loop
        Console.WriteLine()

        pattern = basePattern + "\r?$"
        Console.WriteLine("Attempting to match each line of an input string with '\r?$':")
        match = Regex.Match(input, pattern, RegexOptions.Multiline)
        Do While match.Success
            Console.Write("The {0} played in the {1} in",
                              match.Groups(1).Value, match.Groups(4).Value)
            For Each capture As Capture In match.Groups(5).Captures
                Console.Write(capture.Value)
            Next
            Console.WriteLine(".")

            match = match.NextMatch()
        Loop
        Console.WriteLine()
    End Sub
End Module
' The example displays the following output:
'    Attempting to match the entire input string:
'    
'    Attempting to match each element in a string array:
'    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
'    The Chicago Cubs played in the National League in 1903-present.
'    The Detroit Tigers played in the American League in 1901-present.
'    The New York Giants played in the National League in 1885-1957.
'    The Washington Senators played in the American League in 1901-1960.
'    
'    Attempting to match each line of an input string with '$':
'    
'    Attempting to match each line of an input string with '\r?$':
'    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
'    The Chicago Cubs played in the National League in 1903-present.
'    The Detroit Tigers played in the American League in 1901-present.
'    The New York Giants played in the National League in 1885-1957.
'    The Washington Senators played in the American League in 1901-1960.

仅字符串的开头:\AStart of String Only: \A

\A 定位点指定匹配必须出现在输入字符串的开头。The \A anchor specifies that a match must occur at the beginning of the input string. 它等同于 ^ 定位点,只不过 \A 忽略了 RegexOptions.Multiline 选项。It is identical to the ^ anchor, except that \A ignores the RegexOptions.Multiline option. 因此,在多行的输入字符串中,它只能匹配第一行的开头。Therefore, it can only match the start of the first line in a multiline input string.

以下示例与 ^$ 定位点的示例类似。The following example is similar to the examples for the ^ and $ anchors. 它在正则表达式中使用 \A 定位点,可提取有关某些职业棒球队存在年限的信息。It uses the \A anchor in a regular expression that extracts information about the years during which some professional baseball teams existed. 输入字符串包括五行。The input string includes five lines. 调用 Regex.Matches(String, String, RegexOptions) 方法仅找到输入字符串中与正则表达式模式匹配的第一个子字符串。The call to the Regex.Matches(String, String, RegexOptions) method finds only the first substring in the input string that matches the regular expression pattern. 如示例所示, Multiline 选项不起作用。As the example shows, the Multiline option has no effect.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string input = "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957\n" +
                       "Chicago Cubs, National League, 1903-present\n" +
                       "Detroit Tigers, American League, 1901-present\n" +
                       "New York Giants, National League, 1885-1957\n" +
                       "Washington Senators, American League, 1901-1960\n";

        string pattern = @"\A((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+";

        Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
        while (match.Success)
        {
            Console.Write("The {0} played in the {1} in",
                              match.Groups[1].Value, match.Groups[4].Value);
            foreach (Capture capture in match.Groups[5].Captures)
                Console.Write(capture.Value);

            Console.WriteLine(".");
            match = match.NextMatch();
        }
        Console.WriteLine();
    }
}
// The example displays the following output:
//    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957" + vbCrLf +
                            "Chicago Cubs, National League, 1903-present" + vbCrLf +
                            "Detroit Tigers, American League, 1901-present" + vbCrLf +
                            "New York Giants, National League, 1885-1957" + vbCrLf +
                            "Washington Senators, American League, 1901-1960" + vbCrLf

        Dim pattern As String = "\A((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+"

        Dim match As Match = Regex.Match(input, pattern, RegexOptions.Multiline)
        Do While match.Success
            Console.Write("The {0} played in the {1} in",
                              match.Groups(1).Value, match.Groups(4).Value)
            For Each capture As Capture In match.Groups(5).Captures
                Console.Write(capture.Value)
            Next
            Console.WriteLine(".")
            match = match.NextMatch()
        Loop
        Console.WriteLine()
    End Sub
End Module
' The example displays the following output:
'    The Brooklyn Dodgers played in the National League in 1911, 1912, 1932-1957.

字符串末尾或结束换行之前:\ZEnd of String or Before Ending Newline: \Z

\Z 定位点指定匹配项必须出现在输入字符串的末尾,或出现在输入字符串末尾的 \n 之前。The \Z anchor specifies that a match must occur at the end of the input string, or before \n at the end of the input string. 它等同于 $ 定位点,只不过 \Z 忽略了 RegexOptions.Multiline 选项。It is identical to the $ anchor, except that \Z ignores the RegexOptions.Multiline option. 因此,在多行字符串中,它只能匹配最后一行的末尾,或 \n前的最后一行。Therefore, in a multiline string, it can only match the end of the last line, or the last line before \n.

请注意, \Z\n 匹配但与 \r\n (CR/LF 字符组合)不匹配。Note that \Z matches \n but does not match \r\n (the CR/LF character combination). 若要匹配 CR/LF,请将 \r?\Z 包括到正则表达式模式中。To match CR/LF, include \r?\Z in the regular expression pattern.

以下示例在正则表达式中使用 \Z 定位点,与 字符串或行的开头 部分的示例类似,可提取有关某些职业棒球队存在年限的信息。The following example uses the \Z anchor in a regular expression that is similar to the example in the Start of String or Line section, which extracts information about the years during which some professional baseball teams existed. 正则表达式 \r?\Z 中的子表达式 ^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\Z 匹配字符串的末尾,也匹配以 \n\r\n结尾的字符串。The subexpression \r?\Z in the regular expression ^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\Z matches the end of a string, and also matches a string that ends with \n or \r\n. 因此,数组中的每个元素都与正则表达式模式匹配。As a result, each element in the array matches the regular expression pattern.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string[] inputs = { "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957",
                          "Chicago Cubs, National League, 1903-present" + Environment.NewLine,
                          "Detroit Tigers, American League, 1901-present" + Regex.Unescape(@"\n"),
                          "New York Giants, National League, 1885-1957",
                          "Washington Senators, American League, 1901-1960" + Environment.NewLine};
        string pattern = @"^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\Z";

        foreach (string input in inputs)
        {
            Console.WriteLine(Regex.Escape(input));
            Match match = Regex.Match(input, pattern);
            if (match.Success)
                Console.WriteLine("   Match succeeded.");
            else
                Console.WriteLine("   Match failed.");
        }
    }
}
// The example displays the following output:
//    Brooklyn\ Dodgers,\ National\ League,\ 1911,\ 1912,\ 1932-1957
//       Match succeeded.
//    Chicago\ Cubs,\ National\ League,\ 1903-present\r\n
//       Match succeeded.
//    Detroit\ Tigers,\ American\ League,\ 1901-present\n
//       Match succeeded.
//    New\ York\ Giants,\ National\ League,\ 1885-1957
//       Match succeeded.
//    Washington\ Senators,\ American\ League,\ 1901-1960\r\n
//       Match succeeded.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim inputs() As String = {"Brooklyn Dodgers, National League, 1911, 1912, 1932-1957",
                              "Chicago Cubs, National League, 1903-present" + vbCrLf,
                              "Detroit Tigers, American League, 1901-present" + vbLf,
                              "New York Giants, National League, 1885-1957",
                              "Washington Senators, American League, 1901-1960" + vbCrLf}
        Dim pattern As String = "^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\Z"

        For Each input As String In inputs
            Console.WriteLine(Regex.Escape(input))
            Dim match As Match = Regex.Match(input, pattern)
            If match.Success Then
                Console.WriteLine("   Match succeeded.")
            Else
                Console.WriteLine("   Match failed.")
            End If
        Next
    End Sub
End Module
' The example displays the following output:
'    Brooklyn\ Dodgers,\ National\ League,\ 1911,\ 1912,\ 1932-1957
'       Match succeeded.
'    Chicago\ Cubs,\ National\ League,\ 1903-present\r\n
'       Match succeeded.
'    Detroit\ Tigers,\ American\ League,\ 1901-present\n
'       Match succeeded.
'    New\ York\ Giants,\ National\ League,\ 1885-1957
'       Match succeeded.
'    Washington\ Senators,\ American\ League,\ 1901-1960\r\n
'       Match succeeded.

仅字符串末尾:\zEnd of String Only: \z

\z 定位点指定匹配必须出现在输入字符串末尾。The \z anchor specifies that a match must occur at the end of the input string. $ 语言元素类似, \z 忽略了 RegexOptions.Multiline 选项。Like the $ language element, \z ignores the RegexOptions.Multiline option. \Z 语言元素不同, \z 不匹配字符串末尾的 \n 字符。Unlike the \Z language element, \z does not match a \n character at the end of a string. 因此,它只能匹配输入字符串的最后一行。Therefore, it can only match the last line of the input string.

以下示例在正则表达式中使用 \z 定位点,与上一部分的示例中使用的定位点在其他方面相同,用于提取有关某些职业棒球队存在年限的信息。The following example uses the \z anchor in a regular expression that is otherwise identical to the example in the previous section, which extracts information about the years during which some professional baseball teams existed. 此示例尝试使用正则表达式模式 ^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\z匹配字符串数组中五个元素的每一个。The example tries to match each of five elements in a string array with the regular expression pattern ^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\z. 两个字符串以回车符和换行符结尾,一个字符串以换行符结尾,另外两个既不以回车符也不以换行符结尾。Two of the strings end with carriage return and line feed characters, one ends with a line feed character, and two end with neither a carriage return nor a line feed character. 如输出所示,只有不包含回车符或换行符的字符串与模式匹配。As the output shows, only the strings without a carriage return or line feed character match the pattern.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string[] inputs = { "Brooklyn Dodgers, National League, 1911, 1912, 1932-1957",
                          "Chicago Cubs, National League, 1903-present" + Environment.NewLine,
                          "Detroit Tigers, American League, 1901-present\n",
                          "New York Giants, National League, 1885-1957",
                          "Washington Senators, American League, 1901-1960" + Environment.NewLine };
        string pattern = @"^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\z";

        foreach (string input in inputs)
        {
            Console.WriteLine(Regex.Escape(input));
            Match match = Regex.Match(input, pattern);
            if (match.Success)
                Console.WriteLine("   Match succeeded.");
            else
                Console.WriteLine("   Match failed.");
        }
    }
}
// The example displays the following output:
//    Brooklyn\ Dodgers,\ National\ League,\ 1911,\ 1912,\ 1932-1957
//       Match succeeded.
//    Chicago\ Cubs,\ National\ League,\ 1903-present\r\n
//       Match failed.
//    Detroit\ Tigers,\ American\ League,\ 1901-present\n
//       Match failed.
//    New\ York\ Giants,\ National\ League,\ 1885-1957
//       Match succeeded.
//    Washington\ Senators,\ American\ League,\ 1901-1960\r\n
//       Match failed.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim inputs() As String = {"Brooklyn Dodgers, National League, 1911, 1912, 1932-1957",
                              "Chicago Cubs, National League, 1903-present" + vbCrLf,
                              "Detroit Tigers, American League, 1901-present" + vbLf,
                              "New York Giants, National League, 1885-1957",
                              "Washington Senators, American League, 1901-1960" + vbCrLf}
        Dim pattern As String = "^((\w+(\s?)){2,}),\s(\w+\s\w+),(\s\d{4}(-(\d{4}|present))?,?)+\r?\z"

        For Each input As String In inputs
            Console.WriteLine(Regex.Escape(input))
            Dim match As Match = Regex.Match(input, pattern)
            If match.Success Then
                Console.WriteLine("   Match succeeded.")
            Else
                Console.WriteLine("   Match failed.")
            End If
        Next
    End Sub
End Module
' The example displays the following output:
'    Brooklyn\ Dodgers,\ National\ League,\ 1911,\ 1912,\ 1932-1957
'       Match succeeded.
'    Chicago\ Cubs,\ National\ League,\ 1903-present\r\n
'       Match failed.
'    Detroit\ Tigers,\ American\ League,\ 1901-present\n
'       Match failed.
'    New\ York\ Giants,\ National\ League,\ 1885-1957
'       Match succeeded.
'    Washington\ Senators,\ American\ League,\ 1901-1960\r\n
'       Match failed.

连续匹配:\GContiguous Matches: \G

\G 定位符指定匹配必须出现在上一个匹配结束的点。The \G anchor specifies that a match must occur at the point where the previous match ended. 将此定位点与 Regex.MatchesMatch.NextMatch 方法配合使用时,它可确保所有匹配项是连续的。When you use this anchor with the Regex.Matches or Match.NextMatch method, it ensures that all matches are contiguous.

以下示例使用正则表达式从一个以逗号分隔的字符串中提取啮齿类动物的名称。The following example uses a regular expression to extract the names of rodent species from a comma-delimited string.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string input = "capybara,squirrel,chipmunk,porcupine,gopher," +
                       "beaver,groundhog,hamster,guinea pig,gerbil," +
                       "chinchilla,prairie dog,mouse,rat";
        string pattern = @"\G(\w+\s?\w*),?";
        Match match = Regex.Match(input, pattern);
        while (match.Success)
        {
            Console.WriteLine(match.Groups[1].Value);
            match = match.NextMatch();
        }
    }
}
// The example displays the following output:
//       capybara
//       squirrel
//       chipmunk
//       porcupine
//       gopher
//       beaver
//       groundhog
//       hamster
//       guinea pig
//       gerbil
//       chinchilla
//       prairie dog
//       mouse
//       rat
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "capybara,squirrel,chipmunk,porcupine,gopher," +
                              "beaver,groundhog,hamster,guinea pig,gerbil," +
                              "chinchilla,prairie dog,mouse,rat"
        Dim pattern As String = "\G(\w+\s?\w*),?"
        Dim match As Match = Regex.Match(input, pattern)
        Do While match.Success
            Console.WriteLine(match.Groups(1).Value)
            match = match.NextMatch()
        Loop
    End Sub
End Module
' The example displays the following output:
'       capybara
'       squirrel
'       chipmunk
'       porcupine
'       gopher
'       beaver
'       groundhog
'       hamster
'       guinea pig
'       gerbil
'       chinchilla
'       prairie dog
'       mouse
'       rat

正则表达式 \G(\w+\s?\w*),? 可以解释为下表中所示内容。The regular expression \G(\w+\s?\w*),? is interpreted as shown in the following table.

模式Pattern 描述Description
\G 从上次匹配结束的位置开始。Begin where the last match ended.
\w+ 匹配一个或多个单词字符。Match one or more word characters.
\s? 匹配零个或一个空格。Match zero or one space.
\w* 匹配零个或多个单词字符。Match zero or more word characters.
(\w+\s?\w*) 匹配后跟零个或一个空格再后跟零个或多个单词字符的一个或多个单词字符。Match one or more word characters followed by zero or one space, followed by zero or more word characters. 这是第一个捕获组。This is the first capturing group.
,? 匹配文本逗号字符的零个或一个匹配项。Match zero or one occurrence of a literal comma character.

字边界:\bWord Boundary: \b

\b 定位符指定匹配必须出现单词字符( \w 语言元素)和非单词字符( \W 语言元素)之间的边界上。The \b anchor specifies that the match must occur on a boundary between a word character (the \w language element) and a non-word character (the \W language element). 单词字符包括字母数字字符和下划线;非单词字符包括不为字母数字字符或下划线的任何字符。Word characters consist of alphanumeric characters and underscores; a non-word character is any character that is not alphanumeric or an underscore. (有关详细信息,请参阅字符类。)匹配也可以出现在字符串开头或结尾处的单词边界上。(For more information, see Character Classes.) The match may also occur on a word boundary at the beginning or end of the string.

\b 定位点经常用于确保子表达式与整个单词而不仅与单词的开头或结尾匹配。The \b anchor is frequently used to ensure that a subexpression matches an entire word instead of just the beginning or end of a word. 以下示例中的正则表达式 \bare\w*\b 阐释了这种用法。The regular expression \bare\w*\b in the following example illustrates this usage. 它与任何以子字符串“are”开头的单词匹配。It matches any word that begins with the substring "are". 该示例的输出也阐释了 \b 与输入字符串的开头和结尾均匹配。The output from the example also illustrates that \b matches both the beginning and the end of the input string.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string input = "area bare arena mare";
        string pattern = @"\bare\w*\b";
        Console.WriteLine("Words that begin with 'are':");
        foreach (Match match in Regex.Matches(input, pattern))
            Console.WriteLine("'{0}' found at position {1}",
                              match.Value, match.Index);
    }
}
// The example displays the following output:
//       Words that begin with 'are':
//       'area' found at position 0
//       'arena' found at position 10
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "area bare arena mare"
        Dim pattern As String = "\bare\w*\b"
        Console.WriteLine("Words that begin with 'are':")
        For Each match As Match In Regex.Matches(input, pattern)
            Console.WriteLine("'{0}' found at position {1}",
                              match.Value, match.Index)
        Next
    End Sub
End Module
' The example displays the following output:
'       Words that begin with 'are':
'       'area' found at position 0
'       'arena' found at position 10

正则表达式模式可以解释为下表中所示内容。The regular expression pattern is interpreted as shown in the following table.

模式Pattern 描述Description
\b 在单词边界处开始匹配。Begin the match at a word boundary.
are 匹配子字符串“are”。Match the substring "are".
\w* 匹配零个或多个单词字符。Match zero or more word characters.
\b 在单词边界处结束匹配。End the match at a word boundary.

非字边界:\BNon-Word Boundary: \B

\B 定位符指定匹配不得出现在单词边界上。The \B anchor specifies that the match must not occur on a word boundary. 它与 \b 定位点截然相反。It is the opposite of the \b anchor.

以下示例使用 \B 定位点定位单词中的子字符串“qu”匹配项。The following example uses the \B anchor to locate occurrences of the substring "qu" in a word. 正则表达式模式 \Bqu\w+ 与以“qu”开头(但“qu”并不位于单词之首)且延续到单词末尾的子字符串匹配。The regular expression pattern \Bqu\w+ matches a substring that begins with a "qu" that does not start a word and that continues to the end of the word.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string input = "equity queen equip acquaint quiet";
        string pattern = @"\Bqu\w+";
        foreach (Match match in Regex.Matches(input, pattern))
            Console.WriteLine("'{0}' found at position {1}",
                              match.Value, match.Index);
    }
}
// The example displays the following output:
//       'quity' found at position 1
//       'quip' found at position 14
//       'quaint' found at position 21
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim input As String = "equity queen equip acquaint quiet"
        Dim pattern As String = "\Bqu\w+"
        For Each match As Match In Regex.Matches(input, pattern)
            Console.WriteLine("'{0}' found at position {1}",
                              match.Value, match.Index)
        Next
    End Sub
End Module
' The example displays the following output:
'       'quity' found at position 1
'       'quip' found at position 14
'       'quaint' found at position 21

正则表达式模式可以解释为下表中所示内容。The regular expression pattern is interpreted as shown in the following table.

模式Pattern 描述Description
\B 不在单词边界处开始匹配。Do not begin the match at a word boundary.
qu 匹配子字符串“qu”。Match the substring "qu".
\w+ 匹配一个或多个单词字符。Match one or more word characters.

请参阅See also