代替構成体

代替構成体は、OR 一致または条件一致を有効にするように正規表現を変更します。 .NET Framework では、3 つの代替構成体をサポートしています。

  • | を使用したパターン一致

  • (?(expression)yes|no) を使用した条件一致

  • 有効なキャプチャ グループに基づく条件一致

| を使用したパターン一致

縦棒 (|) 文字を使用すると、 | 文字で各パターンを区切った一連のパターンのいずれかと照合することができます。

文字クラスの肯定と同様に、 | 文字を使用すると、複数の文字のうちのいずれかの 1 文字と照合することができます。 次の例では、文字クラスの肯定と | 文字を使用した OR 一致の両方を使用して、文字列内にある単語 "gray" または "grey" を検索しています。 この例では、 | 文字を使用することで、より詳細な正規表現を作成しています。

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      ' Regular expression using character class.
      Dim pattern1 As String = "\bgr[ae]y\b"
      ' Regular expression using either/or.
      Dim pattern2 As String = "\bgr(a|e)y\b"

      Dim input As String = "The gray wolf blended in among the grey rocks."
      For Each match As Match In Regex.Matches(input, pattern1)
         Console.WriteLine("'{0}' found at position {1}", _
                           match.Value, match.Index)
      Next      
      Console.WriteLine()
      For Each match As Match In Regex.Matches(input, pattern2)
         Console.WriteLine("'{0}' found at position {1}", _
                           match.Value, match.Index)
      Next      
   End Sub
End Module
' The example displays the following output:
'       'gray' found at position 4
'       'grey' found at position 35
'       
'       'gray' found at position 4
'       'grey' found at position 35           
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      // Regular expression using character class.
      string pattern1 = @"\bgr[ae]y\b";
      // Regular expression using either/or.
      string pattern2 = @"\bgr(a|e)y\b";

      string input = "The gray wolf blended in among the grey rocks.";
      foreach (Match match in Regex.Matches(input, pattern1))
         Console.WriteLine("'{0}' found at position {1}", 
                           match.Value, match.Index);
      Console.WriteLine();
      foreach (Match match in Regex.Matches(input, pattern2))
         Console.WriteLine("'{0}' found at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       'gray' found at position 4
//       'grey' found at position 35
//       
//       'gray' found at position 4
//       'grey' found at position 35           

次の表に | 文字を使用した正規表現 \bgr(a|e)y\b の解釈を示します。

パターン

説明

\b

ワード境界から開始します。

gr

文字 "gr" と一致します。

(a|e)

"a" または "e" と一致します。

y\b

ワード境界の "y" と一致します。

| 文字は、複数の文字または部分式を使用した OR 一致を実行する場合にも使用できます。これには、文字リテラルと正規表現言語要素を自由に組み合わせて使用できます (文字クラスにはこの機能はありません)。 次の例では、 | 文字を使用して、 ddd-dd-dddd の形式の 9 桁の米国社会保障番号 (SSN)、または dd-ddddddd の形式の 9 桁の米国雇用者番号 (EIN) のいずれかを抽出します。

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b"
      Dim input As String = "01-9999999 020-333333 777-88-9999"
      Console.WriteLine("Matches for {0}:", pattern)
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
      Next   
   End Sub
End Module
' The example displays the following output:
'       Matches for \b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
'          01-9999999 at position 0
'          777-88-9999 at position 22
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b";
      string input = "01-9999999 020-333333 777-88-9999";
      Console.WriteLine("Matches for {0}:", pattern);
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
   }
}
// The example displays the following output:
//       Matches for \b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

正規表現 \b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b の解釈を次の表に示します。

パターン

説明

\b

ワード境界から開始します。

(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})

2 桁と 7 桁の 10 進数がハイフンでつながれた文字列、または 3 桁、2 桁、4 桁の 10 進数がそれぞれハイフンでつながれた文字列のいずれかと一致します。

\d

ワード境界で照合を終了します。

ページのトップへ

式を使用した条件一致

この言語要素では、最初のパターンに一致するかどうかに応じて、2 つのパターンのいずれかの照合を試行します。 構文は次のとおりです。

(?(expression)yes|no)

expression は照合する最初のパターンです。yes は expression に一致する場合に照合するパターン、no は expression に一致しない場合に照合するパターン (省略可能) です。 正規表現エンジンでは、expression をゼロ幅アサーションとして扱います。つまり、正規表現エンジンは、expression を評価した後、以降の入力ストリームには進みません。 したがって、この構成体は次の構成体と同じになります。

(?(?=expression)yes|no)

(?=expression) はゼロ幅アサーションの構成体です (詳細については、グループ化構成体 のトピックを参照してください)。 正規表現エンジンでは expression をアンカー (ゼロ幅アサーション) として解釈するため、expression は、ゼロ幅アサーション (詳細については、「正規表現のアンカー」を参照してください) か、yes にも含まれる部分式のいずれかにする必要があります。 それ以外の場合、yes のパターンを照合することはできません。

メモメモ

expression が名前付きまたは番号付きのキャプチャ グループの場合、代替構成体はキャプチャのテストとして解釈されます。詳細については、次の「有効なキャプチャ グループに基づく条件一致」を参照してください。言い換えると、正規表現エンジンでは、キャプチャされた部分文字列の照合は試行されず、代わりにグループが存在するかどうかがテストされます。

次の例は、「| を使用したパターン一致」の例の変化形です。 この例では、条件一致を使用して、ワード境界の後の 3 文字が 2 桁の数値とその後のハイフンから成る文字列であるかどうかを確認します。 このパターンに一致する場合は、米国 雇用者番号 (EIN) の照合を試行します。 一致しない場合は、米国 社会保障番号 (SSN) の照合を試行します。

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b"
      Dim input As String = "01-9999999 020-333333 777-88-9999"
      Console.WriteLine("Matches for {0}:", pattern)
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
      Next   
   End Sub
End Module
' The example displays the following output:
'       Matches for \b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
'          01-9999999 at position 0
'          777-88-9999 at position 22
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b";
      string input = "01-9999999 020-333333 777-88-9999";
      Console.WriteLine("Matches for {0}:", pattern);
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
   }
}
// The example displays the following output:
//       Matches for \b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

正規表現パターン \b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b の解釈を次の表に示します。

パターン

説明

\b

ワード境界から開始します。

(?(\d{2}-)

次の 3 文字が 2 桁の数値とその後のハイフンから成る文字列であるかどうかを確認します。

\d{2}-\d{7}

前のパターンに一致した場合に、2 桁と 7 桁の数値がハイフンでつながれた文字列を照合します。

\d{3}-\d{2}-\d{4}

前のパターンに一致しない場合に、3 桁、2 桁、4 桁の 10 進数がそれぞれハイフンでつながれた文字列を照合します。

\b

ワード境界に一致します。

ページのトップへ

有効なキャプチャ グループに基づく条件一致

この言語要素では、指定したキャプチャ グループに一致するかどうかに応じて、2 つのパターンのいずれかの照合を試行します。 構文は次のとおりです。

(?(name)yes|no)

または

(?(number)yes|no)

name はキャプチャ グループの名前、number はキャプチャ グループの番号です。yes は name または number が一致する場合に照合する式、no は一致しない場合に照合する式 (省略可能) です。

name が、正規表現パターンで使用するキャプチャ グループの名前に対応しない場合、前のセクションで説明したように、代替構成体は式のテストとして解釈されます。 通常、これは expression が false になることを意味します。 number が、正規表現パターンで使用する番号付きのキャプチャ グループに対応しない場合は、正規表現エンジンから ArgumentException がスローされます。

次の例は、「| を使用したパターン一致」の例の変化形です。 この例では、2 桁の数値とその後のハイフンから成る n2 という名前のキャプチャ グループを使用しています。 代替構成体は、このキャプチャ グループに一致するものが入力文字列にあるかどうかをテストします。 一致するものがある場合、米国 雇用者番号 (EIN) の照合を試行します。 一致するものがない場合は、米国 社会保障番号 (SSN) の照合を試行します。

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(?<n2>\d{2}-)*(?(n2)\d{7}|\d{3}-\d{2}-\d{4})\b"
      Dim input As String = "01-9999999 020-333333 777-88-9999"
      Console.WriteLine("Matches for {0}:", pattern)
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
      Next   
   End Sub
End Module
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(?<n2>\d{2}-)*(?(n2)\d{7}|\d{3}-\d{2}-\d{4})\b";
      string input = "01-9999999 020-333333 777-88-9999";
      Console.WriteLine("Matches for {0}:", pattern);
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
   }
}
// The example displays the following output:
//       Matches for \b(?<n2>\d{2}-)*(?(n2)\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

正規表現パターン \b(?<n2>\d{2}-)*(?(n2)\d{7}|\d{3}-\d{2}-\d{4})\b の解釈を次の表に示します。

パターン

説明

\b

ワード境界から開始します。

(?<n2>\d{2}-)*

2 桁の数値とそれに続くハイフンに 0 回または 1 回一致します。 このキャプチャ グループに n2 という名前を付けます。

(?(n2)

入力文字列に n2 に一致するものがあるかどうかをテストします。

)\d{7}

n2 に一致するものがある場合に、7 桁の 10 進数を照合します。

|\d{3}-\d{2}-\d{4}

n2 に一致するものがない場合に、3 桁、2 桁、4 桁の 10 進数がそれぞれハイフンでつながれた文字列を照合します。

\b

ワード境界に一致します。

この例の変化形として、名前付きグループの代わりに番号付きグループを使用した例を次に示します。 この正規表現パターンは \b(\d{2}-)*(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b です。

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(\d{2}-)*(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b"
      Dim input As String = "01-9999999 020-333333 777-88-9999"
      Console.WriteLine("Matches for {0}:", pattern)
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
      Next   
   End Sub
End Module
' The example displays the following output:
'       Matches for \b(\d{2}-)*(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b:
'          01-9999999 at position 0
'          777-88-9999 at position 22
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\d{2}-)*(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b";
      string input = "01-9999999 020-333333 777-88-9999";
      Console.WriteLine("Matches for {0}:", pattern);
      foreach (Match match in Regex.Matches(input, pattern))
         Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
   }
}
// The example display the following output:
//       Matches for \b(\d{2}-)*(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

ページのトップへ

参照

その他の技術情報

正規表現言語要素