正規表現のアンカー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 メソッドの 2 つのオーバーロードを呼び出しています。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,} 1 個以上の単語文字の後に 0 個または 1 個の空白が続くパターンが少なくとも 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. この式では、2 番目と 3 番目のキャプチャ グループも定義されます。2 番目はキャプチャ ワードで、3 番目はキャプチャされた空白で構成されます。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+) 1 個以上の単語文字の後に 1 個の空白が続き、さらに 1 個以上の単語文字が続くパターンに一致します。Match one or more word characters followed by a space, followed by one or more word characters. これが 4 番目のキャプチャ グループです。This is the fourth capturing group.
, コンマに一致します。Match a comma.
\s\d{4} 1 個の空白の後に 4 個の 10 進数字が続くパターンに一致します。Match a space followed by four decimal digits.
(-(\d{4}|present))? 1 個のハイフンの後に 4 個の 10 進数字または文字列 "present" が続くパターンの、0 回または 1 回の繰り返しに一致します。Match zero or one occurrence of a hyphen followed by four decimal digits or the string "present". これが 6 番目のキャプチャ グループです。This is the sixth capturing group. 7 番目のキャプチャ グループも含まれています。It also includes a seventh capturing group.
,? コンマの 0 回または 1 回の繰り返しに一致します。Match zero or one occurrence of a comma.
(\s\d{4}(-(\d{4}|present))?,?)+ 1 個のスペース、4 個の 10 進数字、1 個のハイフンの後に 4 個の 10 進数字または文字列 "present" が続くパターンの 0 回または 1 回の繰り返し、0 個または 1 個のコンマが並んだパターンの、1 回以上の繰り返しに一致します。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. これが 5 番目のキャプチャ グループです。This is the fifth capturing group.

ページのトップへBack to top

文字列または行の末尾: $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. 5 つのテキスト行を含む元の入力文字列に対して使用した場合、 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) メソッドは 5 つの各行の一致に成功します。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 メソッドを呼び指すと、正規表現パターンが変更され、再び 5 つの一致が検索されるようになります。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.

ページのトップへBack to top

文字列の先頭のみ: \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. 入力文字列には 5 つの行が含まれます。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.

ページのトップへBack to top

文字列の末尾または末尾の改行の前: \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.

ページのトップへBack to top

文字列の末尾のみ: \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. この例は、文字列配列の 5 つの各要素と正規表現パターン ^((\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. これらのうち、2 つの文字列は復帰文字と改行文字で終わり、別の 1 つの文字列は改行文字で終わっています。残りの 2 つの文字列の末尾には、復帰文字も改行文字もありません。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.

ページのトップへBack to top

連続一致: \GContiguous Matches: \G

\G アンカーは、前回の一致が終了した位置に一致する必要があることを指定します。The \G anchor specifies that a match must occur at the point where the previous match ended. このアンカーを Regex.Matches メソッドまたは Match.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+ 1 つ以上の単語文字に一致します。Match one or more word characters.
\s? 0 個または 1 個の空白に一致します。Match zero or one space.
\w* 0 個以上の単語に使用される文字に一致します。Match zero or more word characters.
(\w+\s?\w*) 1 個以上の単語文字の後に 0 個または 1 個の空白が続き、さらに 0 個以上の単語文字が続くパターンに一致します。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.
,? リテラルのコンマ文字の 0 回または 1 回の繰り返しに一致します。Match zero or one occurrence of a literal comma character.

ページのトップへBack to top

ワード境界: \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* 0 個以上の単語に使用される文字に一致します。Match zero or more word characters.
\b ワード境界で照合を終了します。End the match at a word boundary.

ページのトップへBack to top

ワード境界以外: \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" で始まり (単語の先頭ではない)、単語の終わりまでの部分文字列に一致します。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+ 1 つ以上の単語文字に一致します。Match one or more word characters.

関連項目See also