次の方法で共有


Regex.Split メソッド

定義

正規表現の一致で定義された位置にある部分文字列の配列に入力文字列を分割します。

オーバーロード

Split(String, String, RegexOptions, TimeSpan)

指定した正規表現パターンで定義された位置にある部分文字列の配列に入力文字列を分割します。 追加のパラメーターでは、一致する操作を変更するオプションと、一致するものが見つからない場合はタイムアウト間隔を指定します。

Split(String, String, RegexOptions)

指定した正規表現パターンで定義された位置にある部分文字列の配列に入力文字列を分割します。 指定したオプションによって、一致する操作が変更されます。

Split(String, Int32, Int32)

指定した最大回数の入力文字列を、Regex コンストラクターで指定された正規表現によって定義された位置にある部分文字列の配列に分割します。 正規表現パターンの検索は、入力文字列内の指定した文字位置から開始します。

Split(String, String)

正規表現パターンで定義された位置にある部分文字列の配列に入力文字列を分割します。

Split(String)

入力文字列を、Regex コンストラクターで指定された正規表現パターンで定義された位置にある部分文字列の配列に分割します。

Split(String, Int32)

指定した最大回数の入力文字列を、Regex コンストラクターで指定された正規表現によって定義された位置にある部分文字列の配列に分割します。

Split(String, String, RegexOptions, TimeSpan)

ソース:
Regex.Split.cs
ソース:
Regex.Split.cs
ソース:
Regex.Split.cs

指定した正規表現パターンで定義された位置にある部分文字列の配列に入力文字列を分割します。 追加のパラメーターでは、一致する操作を変更するオプションと、一致するものが見つからない場合はタイムアウト間隔を指定します。

public:
 static cli::array <System::String ^> ^ Split(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static string[] Split (string input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member Split : string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> string[]
Public Shared Function Split (input As String, pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As String()

パラメーター

input
String

分割する文字列。

pattern
String

照合する正規表現パターン。

options
RegexOptions

一致するオプションを提供する列挙値のビットごとの組み合わせ。

matchTimeout
TimeSpan

タイムアウト間隔、またはメソッドがタイムアウトしないことを示す InfiniteMatchTimeout

戻り値

String[]

文字列配列。

例外

正規表現解析エラーが発生しました。

input または patternnull

options は、RegexOptions 値の有効なビットごとの組み合わせではありません。

-又は-

matchTimeout が負、ゼロ、または約 24 日を超えています。

タイムアウトが発生しました。 タイムアウトの詳細については、「解説」セクションを参照してください。

注釈

Regex.Split メソッドは、String.Split(Char[]) メソッドに似ていますが、Regex.Split は、文字列を一連の文字ではなく正規表現によって決定された区切り記号で分割する点が異なります。 文字列はできるだけ多くの回数分割されます。 区切り記号が見つからない場合、戻り値には、値が元の input 文字列である 1 つの要素が含まれます。

pattern パラメーターは、一致する文字列を記号的に記述する正規表現言語要素で構成されます。 正規表現の詳細については、「.NET 正規表現 正規表現言語 - クイック リファレンス」を参照してください。

大事な

静的 Split メソッドの呼び出しで使用されるコンパイル済みの正規表現は、自動的にキャッシュされます。 コンパイルされた正規表現の有効期間を自分で管理するには、インスタンス Split メソッドを使用します。

複数の一致が互いに隣接している場合は、空の文字列が配列に挿入されます。 たとえば、1 つのハイフンで文字列を分割すると、2 つの隣接するハイフンが見つかった位置に、返された配列に空の文字列が含まれます。

入力文字列の先頭または末尾に一致が見つかった場合は、返された配列の先頭または末尾に空の文字列が含まれます。 次の例では、正規表現パターン [a-z]+ を使用して、大文字または小文字の英字で入力文字列を分割します。 文字列は一致する英字で始まり、終わるので、返される配列の最初と最後の要素の値は String.Empty

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "[a-z]+";
      string input = "Abc1234Def5678Ghi9012Jklm";
      string[] result = Regex.Split(input, pattern, 
                                    RegexOptions.IgnoreCase,
                                    TimeSpan.FromMilliseconds(500));
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', '1234', '5678', '9012', ''
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "[a-z]+"
      Dim input As String = "Abc1234Def5678Ghi9012Jklm"
      Dim result() As String = Regex.Split(input, pattern, 
                                           RegexOptions.IgnoreCase,
                                           TimeSpan.FromMilliseconds(500))
      For ctr As Integer = 0 To result.Length - 1
         Console.Write("'{0}'", result(ctr))
         If ctr < result.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine()
   End Sub               
End Module
' The example displays the following output:
'       '', '1234', '5678', '9012', ''

キャプチャかっこを Regex.Split 式で使用する場合は、キャプチャされたテキストが結果の文字列配列に含まれます。 たとえば、キャプチャかっこ内に配置されたハイフンに文字列 "plum-pear" を分割した場合、返される配列にはハイフンを含む文字列要素が含まれます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum-pear";
      string pattern = "(-)";

      string[] substrings = Regex.Split(input, pattern);    // Split on hyphens
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//    'plum'
//    '-'
//    'pear'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "plum-pear"
      Dim pattern As String = "(-)" 
      
      Dim substrings() As String = Regex.Split(input, pattern)    ' Split on hyphens.
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The method writes the following to the console:
'    'plum'
'    '-'
'    'pear'

ただし、正規表現パターンに複数のキャプチャかっこセットが含まれている場合、このメソッドの動作は .NET Framework のバージョンによって異なります。 .NET Framework 1.0 および 1.1 では、キャプチャかっこの最初のセット内に一致するものが見つからない場合、追加のキャプチャかっこからキャプチャされたテキストは、返される配列に含まれません。 .NET Framework 2.0 以降では、キャプチャされたすべてのテキストも返された配列に追加されます。 たとえば、次のコードでは、2 組のキャプチャかっこを使用して、日付文字列から日付の区切り記号を含む日付の要素を抽出します。 かっこをキャプチャする最初のセットはハイフンをキャプチャし、2 番目のセットはスラッシュをキャプチャします。 サンプル コードがコンパイルされ、.NET Framework 1.0 または 1.1 で実行される場合、スラッシュ文字は除外されます。コンパイルされ、.NET Framework 2.0 以降のバージョンで実行される場合は、それらを含みます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";

      foreach (string result in Regex.Split(input, pattern)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// In .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// In .NET 2.0 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "07/14/2007"   
      Dim pattern As String = "(-)|(/)"
      For Each result As String In Regex.Split(input, pattern) 
         Console.WriteLine("'{0}'", result)
      Next
   End Sub
End Module
' In .NET 1.0 and 1.1, the method returns an array of
' 3 elements, as follows:
'    '07'
'    '14'
'    '2007'
'
' In .NET 2.0 and later, the method returns an array of
' 5 elements, as follows:
'    '07'
'    '/'
'    '14'
'    '/'
'    '2007'

正規表現が空の文字列と一致する場合、Split は、空の文字列区切り記号がすべての場所で見つかるので、文字列を 1 文字の文字列の配列に分割します。

matchTimeout パラメーターは、パターン マッチング メソッドがタイムアウトするまでの一致の検索を試行する期間を指定します。タイムアウト間隔を設定すると、過剰なバックトラッキングに依存する正規表現が、近い一致を含む入力を処理するときに応答を停止するように見えるのを防ぐことができます。 詳細については、「正規表現の とバックトラッキングベスト プラクティス」を参照してください。 その時間間隔で一致するものが見つからない場合、メソッドは RegexMatchTimeoutException 例外をスローします。 matchTimeout は、メソッドが実行されるアプリケーション ドメインに対して定義されている既定のタイムアウト値をオーバーライドします。

注意 (呼び出し元)

matchTimeout パラメーターを適切な値 (2 秒など) に設定することをお勧めします。 InfiniteMatchTimeoutを指定してタイムアウトを無効にすると、正規表現エンジンのパフォーマンスが若干向上します。 ただし、タイムアウトは次の条件でのみ無効にする必要があります。

  • 正規表現によって処理される入力が既知の信頼できるソースから派生した場合、または静的テキストで構成されている場合。 これにより、ユーザーによって動的に入力されたテキストが除外されます。

  • 正規表現パターンが十分にテストされ、一致、一致しない、および近い一致が効率的に処理されるようにする場合。

  • 正規表現パターンに、近い一致を処理するときに過剰なバックトラッキングを引き起こすことがわかっている言語要素が含まれている場合。

こちらもご覧ください

  • 正規表現言語要素 を する

適用対象

Split(String, String, RegexOptions)

ソース:
Regex.Split.cs
ソース:
Regex.Split.cs
ソース:
Regex.Split.cs

指定した正規表現パターンで定義された位置にある部分文字列の配列に入力文字列を分割します。 指定したオプションによって、一致する操作が変更されます。

public:
 static cli::array <System::String ^> ^ Split(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static string[] Split (string input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member Split : string * string * System.Text.RegularExpressions.RegexOptions -> string[]
Public Shared Function Split (input As String, pattern As String, options As RegexOptions) As String()

パラメーター

input
String

分割する文字列。

pattern
String

照合する正規表現パターン。

options
RegexOptions

一致するオプションを提供する列挙値のビットごとの組み合わせ。

戻り値

String[]

文字列の配列。

例外

正規表現解析エラーが発生しました。

input または patternnull

options は、RegexOptions 値の有効なビットごとの組み合わせではありません。

タイムアウトが発生しました。 タイムアウトの詳細については、「解説」セクションを参照してください。

注釈

Regex.Split メソッドは、String.Split(Char[]) メソッドに似ていますが、Regex.Split は、文字列を一連の文字ではなく正規表現によって決定された区切り記号で分割する点が異なります。 文字列はできるだけ多くの回数分割されます。 区切り記号が見つからない場合、戻り値には、値が元の input 文字列である 1 つの要素が含まれます。

pattern パラメーターは、一致する文字列を記号的に記述する正規表現言語要素で構成されます。 正規表現の詳細については、「.NET 正規表現 正規表現言語 - クイック リファレンス」を参照してください。

大事な

静的 Split メソッドの呼び出しで使用されるコンパイル済みの正規表現は、自動的にキャッシュされます。 コンパイルされた正規表現の有効期間を自分で管理するには、インスタンス Split メソッドを使用します。

複数の一致が互いに隣接している場合は、空の文字列が配列に挿入されます。 たとえば、1 つのハイフンで文字列を分割すると、2 つの隣接するハイフンが見つかった位置に、返された配列に空の文字列が含まれます。

入力文字列の先頭または末尾に一致が見つかった場合は、返された配列の先頭または末尾に空の文字列が含まれます。 次の例では、正規表現パターン [a-z]+ を使用して、大文字または小文字の英字で入力文字列を分割します。 文字列は一致する英字で始まり、終わるので、返される配列の最初と最後の要素の値は String.Empty

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "[a-z]+";
      string input = "Abc1234Def5678Ghi9012Jklm";
      string[] result = Regex.Split(input, pattern, 
                                    RegexOptions.IgnoreCase);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', '1234', '5678', '9012', ''
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "[a-z]+"
      Dim input As String = "Abc1234Def5678Ghi9012Jklm"
      Dim result() As String = Regex.Split(input, pattern, 
                                           RegexOptions.IgnoreCase)
      For ctr As Integer = 0 To result.Length - 1
         Console.Write("'{0}'", result(ctr))
         If ctr < result.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine()
   End Sub               
End Module
' The example displays the following output:
'       '', '1234', '5678', '9012', ''

キャプチャかっこを Regex.Split 式で使用する場合は、キャプチャされたテキストが結果の文字列配列に含まれます。 たとえば、キャプチャかっこ内に配置されたハイフンに文字列 "plum-pear" を分割した場合、返される配列にはハイフンを含む文字列要素が含まれます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum-pear";
      string pattern = "(-)";

      string[] substrings = Regex.Split(input, pattern);    // Split on hyphens
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//    'plum'
//    '-'
//    'pear'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "plum-pear"
      Dim pattern As String = "(-)" 
      
      Dim substrings() As String = Regex.Split(input, pattern)    ' Split on hyphens.
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The method writes the following to the console:
'    'plum'
'    '-'
'    'pear'

ただし、正規表現パターンに複数のキャプチャかっこセットが含まれている場合、このメソッドの動作は .NET Framework のバージョンによって異なります。 .NET Framework 1.0 および 1.1 では、キャプチャかっこの最初のセット内に一致するものが見つからない場合、追加のキャプチャかっこからキャプチャされたテキストは、返される配列に含まれません。 .NET Framework 2.0 以降では、キャプチャされたすべてのテキストも返された配列に追加されます。 たとえば、次のコードでは、2 組のキャプチャかっこを使用して、日付文字列から日付の区切り記号を含む日付の要素を抽出します。 かっこをキャプチャする最初のセットはハイフンをキャプチャし、2 番目のセットはスラッシュをキャプチャします。 サンプル コードがコンパイルされ、.NET Framework 1.0 または 1.1 で実行される場合、スラッシュ文字は除外されます。コンパイルされ、.NET Framework 2.0 以降のバージョンで実行される場合は、それらを含みます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";

      foreach (string result in Regex.Split(input, pattern)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// In .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// In .NET 2.0 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "07/14/2007"   
      Dim pattern As String = "(-)|(/)"
      For Each result As String In Regex.Split(input, pattern) 
         Console.WriteLine("'{0}'", result)
      Next
   End Sub
End Module
' In .NET 1.0 and 1.1, the method returns an array of
' 3 elements, as follows:
'    '07'
'    '14'
'    '2007'
'
' In .NET 2.0 and later, the method returns an array of
' 5 elements, as follows:
'    '07'
'    '/'
'    '14'
'    '/'
'    '2007'

正規表現が空の文字列と一致する場合、Split は、空の文字列区切り記号がすべての場所で見つかるので、文字列を 1 文字の文字列の配列に分割します。

分割操作の実行時間が、メソッドが呼び出されるアプリケーション ドメインに指定されたタイムアウト間隔を超えると、RegexMatchTimeoutException 例外がスローされます。 アプリケーション ドメインのプロパティでタイムアウトが定義されていない場合、またはタイムアウト値が Regex.InfiniteMatchTimeout場合、例外はスローされません。

注意 (呼び出し元)

このメソッドは、メソッドが呼び出されるアプリケーション ドメインの既定のタイムアウト値と等しい間隔の後にタイムアウトします。 アプリケーション ドメインにタイムアウト値が定義されていない場合は、メソッドのタイムアウトを妨げる値 InfiniteMatchTimeoutが使用されます。 パターン一致でテキストを分割するための推奨される静的メソッドは Split(String, String, RegexOptions, TimeSpan)です。これにより、タイムアウト間隔を設定できます。

こちらもご覧ください

  • 正規表現言語要素 を する

適用対象

Split(String, Int32, Int32)

ソース:
Regex.Split.cs
ソース:
Regex.Split.cs
ソース:
Regex.Split.cs

指定した最大回数の入力文字列を、Regex コンストラクターで指定された正規表現によって定義された位置にある部分文字列の配列に分割します。 正規表現パターンの検索は、入力文字列内の指定した文字位置から開始します。

public:
 cli::array <System::String ^> ^ Split(System::String ^ input, int count, int startat);
public string[] Split (string input, int count, int startat);
member this.Split : string * int * int -> string[]
Public Function Split (input As String, count As Integer, startat As Integer) As String()

パラメーター

input
String

分割する文字列。

count
Int32

分割を実行できる最大回数。

startat
Int32

検索を開始する入力文字列内の文字位置。

戻り値

String[]

文字列の配列。

例外

inputnullです。

startat が 0 未満か、inputの長さより大きい値です。

タイムアウトが発生しました。 タイムアウトの詳細については、「解説」セクションを参照してください。

注釈

Regex.Split メソッドは、String.Split メソッドに似ていますが、Regex.Split は、文字列を一連の文字ではなく正規表現によって決定された区切り記号で分割する点が異なります。 count パラメーターは、input 文字列が分割される部分文字列の最大数を指定します。最後の文字列には、文字列の分割されていない剰余が含まれています。 count の値が 0 の場合、可能な限り多くの時間を分割する既定の動作が提供されます。 startat パラメーターは、最初の区切り記号の検索を開始する位置を定義します (先頭の空白をスキップするために使用できます)。

startatの詳細については、Match(String, Int32)の「解説」セクションを参照してください。

文字列内の count+1 位置から一致するものが見つからない場合、メソッドは、input 文字列を含む 1 要素配列を返します。 1 つ以上の一致が見つかった場合、返される配列の最初の要素には、最初の文字から一致する前の 1 文字までの文字列の最初の部分が含まれます。

複数の一致が互いに隣接していて、検出された一致の数が少なくとも 2 count未満の場合、空の文字列が配列に挿入されます。 同様に、文字列の最初の文字である startatで一致が見つかった場合、返される配列の最初の要素は空の文字列です。 つまり、隣接する一致結果の空の文字列は、一致する部分文字列の数が count等しいかどうかを判断する際にカウントされます。 次の例では、正規表現 \d+ を使用して、文字列内の数値の最初の部分文字列の開始位置を検索し、その位置から最大 3 回文字列を分割します。 正規表現パターンは入力文字列の先頭と一致するため、返される文字列配列は空の文字列、5 文字のアルファベット文字列、および文字列の残りの部分で構成されます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJ789KLMNO012PQRST";
      Match m = rgx.Match(input);
      if (m.Success) { 
         int startAt = m.Index;
         string[] result = rgx.Split(input, 3, startAt);
         for (int ctr = 0; ctr < result.Length; ctr++) {
            Console.Write("'{0}'", result[ctr]);
            if (ctr < result.Length - 1)
               Console.Write(", ");
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL789MNOPQ012'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim rgx As New Regex(pattern)
      Dim input As String = "123ABCDE456FGHIJ789KLMNO012PQRST"
      Dim m As Match = rgx.Match(input)
      If m.Success Then 
         Dim startAt As Integer = m.Index
         Dim result() As String = rgx.Split(input, 3, startAt)
         For ctr As Integer = 0 To result.Length - 1
            Console.Write("'{0}'", result(ctr))
            If ctr < result.Length - 1 Then Console.Write(", ")
         Next
         Console.WriteLine()
      End If
   End Sub               
End Module
' The example displays the following output:
'       '', 'ABCDE', 'FGHIJKL789MNOPQ012'

かっこのキャプチャが正規表現で使用されている場合、キャプチャされたテキストは分割文字列の配列に含まれます。 ただし、キャプチャされたテキストを含む配列要素は、一致する数が countに達したかどうかを判断する際にはカウントされません。 たとえば、文字列 '"apple-apricot-plum-pear-pomegranate-pomegranate-peach" を文字列内の文字 15 から始まる最大 4 つの部分文字列に分割すると、次のコードに示すように 7 要素配列になります。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)";
      string input = "apple-apricot-plum-pear-pomegranate-pineapple-peach";

      // Split on hyphens from 15th character on
      Regex regex = new Regex(pattern);    
      // Split on hyphens from 15th character on
      string[] substrings = regex.Split(input, 4, 15);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The method writes the following to the console:
//    'apple-apricot-plum'
//    '-'
//    'pear'
//    '-'
//    'pomegranate'
//    '-'
//    'pineapple-peach'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(-)"
      Dim input As String = "apple-apricot-plum-pear-pomegranate-pineapple-peach"

      Dim regex As Regex = New Regex(pattern)    
      ' Split on hyphens from 15th character on
      Dim substrings() As String = regex.Split(input, 4, 15)
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub  
End Module
' The example displays the following output:
'    'apple-apricot-plum'
'    '-'
'    'pear'
'    '-'
'    'pomegranate'
'    '-'
'    'pineapple-peach'

ただし、正規表現パターンに複数のキャプチャかっこセットが含まれている場合、このメソッドの動作は .NET Framework のバージョンによって異なります。 .NET Framework 1.0 および 1.1 では、キャプチャかっこの最初のセット内に一致するものが見つからない場合、追加のキャプチャかっこからキャプチャされたテキストは返される配列に含まれません。 .NET Framework 2.0 以降では、キャプチャされたすべてのテキストも返された配列に追加されます。 たとえば、次のコードでは、2 組のキャプチャかっこを使用して、文字列内の個々の単語を抽出します。 かっこをキャプチャする最初のセットはハイフンをキャプチャし、2 番目のセットは垂直バーをキャプチャします。 サンプル コードがコンパイルされ、.NET Framework 1.0 または 1.1 で実行される場合、垂直バー文字は除外されます。コンパイルされ、.NET Framework 2.0 以降のバージョンで実行される場合は、それらを含みます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)|([|])";     // possible delimiters found in string
      string input = "apple|apricot|plum|pear|pomegranate|pineapple|peach";

      Regex regex = new Regex(pattern);    
      // Split on delimiters from 15th character on
      string[] substrings = regex.Split(input, 4, 15);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// In .NET 2.0 and later, the method returns an array of
// 7 elements, as follows:
//    apple|apricot|plum'
//    '|'
//    'pear'
//    '|'
//    'pomegranate'
//    '|'
//    'pineapple|peach'
// In .NET 1.0 and 1.1, the method returns an array of
// 4 elements, as follows:
//    'apple|apricot|plum'
//    'pear'
//    'pomegranate'
//    'pineapple|peach'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(-)|([|])"   ' possible delimiters found in string
      Dim input As String = "apple|apricot|plum|pear|pomegranate|pineapple|peach"

      Dim regex As Regex = New Regex(pattern)    
      ' Split on delimiters from 15th character on
      Dim substrings() As String = regex.Split(input, 4, 15)
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' In .NET 2.0, the method returns an array of
' 7 elements, as follows:
'    apple|apricot|plum'
'    '|'
'    'pear'
'    '|'
'    'pomegranate'
'    '|'
'    'pineapple|peach'
' In .NET 1.0 and 1.1, the method returns an array of
' 4 elements, as follows:
'    'apple|apricot|plum'
'    'pear'
'    'pomegranate'
'    'pineapple|peach'

正規表現が空の文字列と一致する場合、Split は、空の文字列区切り記号がすべての場所で見つかるので、文字列を 1 文字の文字列の配列に分割します。 次の使用例は、文字列 "characters" を入力文字列に含まれる要素の数に分割し、文字 "a" で始まります。 null 文字列は入力文字列の末尾と一致するため、返された配列の末尾に null 文字列が挿入されます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      Regex regex = new Regex("");
      string[] substrings = regex.Split(input, input.Length, input.IndexOf("a"));
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write(substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
// The example displays the following output:   
//    {, c, h, a, r, a, c, t, e, rs}
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "characters"
      Dim regex As New Regex("")
      Dim substrings() As String = regex.Split(input, input.Length, _
                                               input.IndexOf("a"))
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write(substrings(ctr))
         If ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example produces the following output:   
'    {, c, h, a, r, a, c, t, e, rs}

分割操作の実行時間が、Regex.Regex(String, RegexOptions, TimeSpan) コンストラクターで指定されたタイムアウト間隔を超えると、RegexMatchTimeoutException 例外がスローされます。 コンストラクターを呼び出すときにタイムアウト間隔を設定しない場合、Regex オブジェクトが作成されるアプリケーション ドメインに対して確立されたタイムアウト値を操作が超えた場合、例外がスローされます。 Regex コンストラクター呼び出しまたはアプリケーション ドメインのプロパティでタイムアウトが定義されていない場合、またはタイムアウト値が Regex.InfiniteMatchTimeout場合、例外はスローされません

こちらもご覧ください

適用対象

Split(String, String)

ソース:
Regex.Split.cs
ソース:
Regex.Split.cs
ソース:
Regex.Split.cs

正規表現パターンで定義された位置にある部分文字列の配列に入力文字列を分割します。

public:
 static cli::array <System::String ^> ^ Split(System::String ^ input, System::String ^ pattern);
public static string[] Split (string input, string pattern);
static member Split : string * string -> string[]
Public Shared Function Split (input As String, pattern As String) As String()

パラメーター

input
String

分割する文字列。

pattern
String

照合する正規表現パターン。

戻り値

String[]

文字列の配列。

例外

正規表現解析エラーが発生しました。

input または patternnull

タイムアウトが発生しました。 タイムアウトの詳細については、「解説」セクションを参照してください。

注釈

Regex.Split メソッドは、String.Split メソッドに似ていますが、Regex.Split は、文字列を一連の文字ではなく正規表現によって決定された区切り記号で分割する点が異なります。 input 文字列は、できるだけ多くの回数分割されます。 input 文字列に pattern が見つからない場合、戻り値には元の input 文字列の値を持つ 1 つの要素が含まれます。

pattern パラメーターは、一致する文字列を記号的に記述する正規表現言語要素で構成されます。 正規表現の詳細については、「.NET 正規表現 正規表現言語 - クイック リファレンス」を参照してください。

大事な

静的 Split メソッドの呼び出しで使用されるコンパイル済みの正規表現は、自動的にキャッシュされます。 コンパイルされた正規表現の有効期間を自分で管理するには、インスタンス Split メソッドを使用します。

複数の一致が互いに隣接している場合は、空の文字列が配列に挿入されます。 たとえば、1 つのハイフンで文字列を分割すると、次のコードに示すように、2 つの隣接するハイフンが見つかった位置に、返された配列に空の文字列が含まれます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum--pear";
      string pattern = "-";            // Split on hyphens
      
      string[] substrings = Regex.Split(input, pattern);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The method displays the following output:
//    'plum'
//    ''
//    'pear'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "plum--pear"
      Dim pattern As String = "-"          ' Split on hyphens
      
      Dim substrings() As String = Regex.Split(input, pattern)
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub  
End Module
' The example displays the following output:
'    'plum'
'    ''
'    'pear'

入力文字列の先頭または末尾に一致が見つかった場合は、返された配列の先頭または末尾に空の文字列が含まれます。 次の例では、正規表現パターンの \d+ を使用して、入力文字列を数値で分割します。 文字列は一致する数値で始まり、終わるので、返される配列の最初と最後の要素の値は String.Empty

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      string[] result = Regex.Split(input, pattern);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim input As String = "123ABCDE456FGHIJKL789MNOPQ012"
      Dim result() As String = Regex.Split(input, pattern)
      For ctr As Integer = 0 To result.Length - 1
         Console.Write("'{0}'", result(ctr))
         If ctr < result.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine()
   End Sub               
End Module
' The example displays the following output:
'       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''

キャプチャかっこを Regex.Split 式で使用する場合は、キャプチャされたテキストが結果の文字列配列に含まれます。 たとえば、キャプチャかっこ内に配置されたハイフンに文字列 "plum-pear" を分割した場合、返される配列にはハイフンを含む文字列要素が含まれます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum-pear";
      string pattern = "(-)";

      string[] substrings = Regex.Split(input, pattern);    // Split on hyphens
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//    'plum'
//    '-'
//    'pear'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "plum-pear"
      Dim pattern As String = "(-)" 
      
      Dim substrings() As String = Regex.Split(input, pattern)    ' Split on hyphens.
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The method writes the following to the console:
'    'plum'
'    '-'
'    'pear'

ただし、正規表現パターンに複数のキャプチャかっこセットが含まれている場合、このメソッドの動作は .NET Framework のバージョンによって異なります。 .NET Framework 1.0 および 1.1 では、キャプチャかっこの最初のセット内に一致するものが見つからない場合、追加のキャプチャかっこからキャプチャされたテキストは、返される配列に含まれません。 .NET Framework 2.0 以降では、キャプチャされたすべてのテキストも返された配列に追加されます。 たとえば、次のコードでは、2 組のキャプチャかっこを使用して、日付文字列から日付の区切り記号を含む日付の要素を抽出します。 かっこをキャプチャする最初のセットはハイフンをキャプチャし、2 番目のセットはスラッシュをキャプチャします。 サンプル コードがコンパイルされ、.NET Framework 1.0 または 1.1 で実行される場合、スラッシュ文字は除外されます。コンパイルされ、.NET Framework 2.0 以降のバージョンで実行される場合は、それらを含みます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";

      foreach (string result in Regex.Split(input, pattern)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// In .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// In .NET 2.0 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "07/14/2007"   
      Dim pattern As String = "(-)|(/)"
      For Each result As String In Regex.Split(input, pattern) 
         Console.WriteLine("'{0}'", result)
      Next
   End Sub
End Module
' In .NET 1.0 and 1.1, the method returns an array of
' 3 elements, as follows:
'    '07'
'    '14'
'    '2007'
'
' In .NET 2.0 and later, the method returns an array of
' 5 elements, as follows:
'    '07'
'    '/'
'    '14'
'    '/'
'    '2007'

正規表現が空の文字列と一致する場合、Split は、空の文字列区切り記号がすべての場所で見つかるので、文字列を 1 文字の文字列の配列に分割します。 例えば:

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      string[] substrings = Regex.Split(input, "");
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write("'{0}'", substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
// The example produces the following output:   
//    {'', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ''}
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "characters"
      Dim substrings() As String = Regex.Split(input, "")
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write("'{0}'", substrings(ctr))
         If ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example produces the following output:   
'    {'', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ''}

返される配列には、配列の先頭と末尾に空の文字列も含まれていることに注意してください。

分割操作の実行時間が、メソッドが呼び出されるアプリケーション ドメインに指定されたタイムアウト間隔を超えると、RegexMatchTimeoutException 例外がスローされます。 アプリケーション ドメインのプロパティでタイムアウトが定義されていない場合、またはタイムアウト値が Regex.InfiniteMatchTimeout場合、例外はスローされません。

注意 (呼び出し元)

このメソッドは、メソッドが呼び出されるアプリケーション ドメインの既定のタイムアウト値と等しい間隔の後にタイムアウトします。 アプリケーション ドメインにタイムアウト値が定義されていない場合は、メソッドのタイムアウトを妨げる値 InfiniteMatchTimeoutが使用されます。 パターン一致でテキストを分割するための推奨される静的メソッドは Split(String, String, RegexOptions, TimeSpan)です。これにより、タイムアウト間隔を設定できます。

こちらもご覧ください

  • 正規表現言語要素 を する

適用対象

Split(String)

ソース:
Regex.Split.cs
ソース:
Regex.Split.cs
ソース:
Regex.Split.cs

入力文字列を、Regex コンストラクターで指定された正規表現パターンで定義された位置にある部分文字列の配列に分割します。

public:
 cli::array <System::String ^> ^ Split(System::String ^ input);
public string[] Split (string input);
member this.Split : string -> string[]
Public Function Split (input As String) As String()

パラメーター

input
String

分割する文字列。

戻り値

String[]

文字列の配列。

例外

inputnullです。

タイムアウトが発生しました。 タイムアウトの詳細については、「解説」セクションを参照してください。

注釈

Regex.Split メソッドは、String.Split(Char[]) メソッドに似ていますが、Regex.Split は、文字列を一連の文字ではなく正規表現によって決定された区切り記号で分割する点が異なります。 文字列はできるだけ多くの回数分割されます。 区切り記号が見つからない場合、戻り値には、値が元の入力文字列である 1 つの要素が含まれます。

複数の一致が互いに隣接している場合は、空の文字列が配列に挿入されます。 たとえば、1 つのハイフンで文字列を分割すると、次のコードに示すように、2 つの隣接するハイフンが見つかった位置に、返された配列に空の文字列が含まれます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      Regex regex = new Regex("-");         // Split on hyphens.
      string[] substrings = regex.Split("plum--pear");
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//    'plum'
//    ''
//    'pear'
Imports System.Text.RegularExpressions

Module RegexSplit
   Public Sub Main()
      Dim regex As Regex = New Regex("-")         ' Split on hyphens.
      Dim substrings() As String = regex.Split("plum--pear")
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The example displays the following output:
'    'plum'
'    ''
'    'pear'

入力文字列の先頭または末尾に一致が見つかった場合は、返された配列の先頭または末尾に空の文字列が含まれます。 次の例では、正規表現パターンの \d+ を使用して、入力文字列を数値で分割します。 文字列は一致する数値で始まり、終わるので、返される配列の最初と最後の要素の値は String.Empty

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      
      string[] result = rgx.Split(input);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim rgx As New Regex(pattern)
      Dim input As String = "123ABCDE456FGHIJKL789MNOPQ012"
      
      Dim result() As String = rgx.Split(input)
      For ctr As Integer = 0 To result.Length - 1
         Console.Write("'{0}'", result(ctr))
         If ctr < result.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine()
   End Sub               
End Module
' The example displays the following output:
'       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''

キャプチャかっこを Regex.Split 式で使用する場合は、キャプチャされたテキストが結果の文字列配列に含まれます。 たとえば、キャプチャかっこ内に配置されたハイフンに文字列 "plum-pear" を分割した場合、返される配列にはハイフンを含む文字列要素が含まれます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      Regex regex = new Regex("(-)");         // Split on hyphens.
      string[] substrings = regex.Split("plum-pear");
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//    'plum'
//    '-'
//    'pear'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim regex As Regex = New Regex("(-)")          ' Split on hyphens.
      Dim substrings() As String = regex.Split("plum-pear")
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The example displays the following output:
'    'plum'
'    '-'
'    'pear'

ただし、正規表現パターンに複数のキャプチャかっこセットが含まれている場合、このメソッドの動作は .NET Framework のバージョンによって異なります。 .NET Framework 1.0 および 1.1 では、キャプチャかっこの最初のセット内に一致するものが見つからない場合、追加のキャプチャかっこからキャプチャされたテキストは、返される配列に含まれません。 .NET Framework 2.0 以降では、キャプチャされたすべてのテキストも返された配列に追加されます。 たとえば、次のコードでは、2 組のキャプチャかっこを使用して、日付文字列から日付の区切り記号を含む日付の要素を抽出します。 かっこをキャプチャする最初のセットはハイフンをキャプチャし、2 番目のセットはスラッシュをキャプチャします。 サンプル コードがコンパイルされ、.NET Framework 1.0 または 1.1 で実行される場合、スラッシュ文字は除外されます。コンパイルされ、.NET Framework 2.0 以降のバージョンで実行される場合は、それらを含みます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";
      Regex regex = new Regex(pattern);
      foreach (string result in regex.Split(input)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// Under .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// Under .NET 2.0 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "07/14/2007"   
      Dim pattern As String = "(-)|(/)"
      Dim regex As Regex = New Regex(pattern)
      For Each result As String In regex.Split(input) 
         Console.WriteLine("'{0}'", result)
      Next
   End Sub
End Module
' In .NET 1.0 and 1.1, the method returns an array of
' 3 elements, as follows:
'    '07'
'    '14'
'    '2007'
'
' In .NET 2.0 and later, the method returns an array of
' 5 elements, as follows:
'    '07'
'    '/'
'    '14'
'    '/'
'    '2007'

正規表現が空の文字列と一致する場合、Split(String) は、空の文字列区切り記号がすべての場所で見つかるので、文字列を 1 文字の文字列の配列に分割します。 例えば:

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      Regex regex = new Regex("");
      string[] substrings = regex.Split(input);
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write(substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
// The example displays the following output:   
//    {, c, h, a, r, a, c, t, e, r, s, }
Imports System.Text.RegularExpressions

Module Main
   Public Sub Main()
      Dim input As String = "characters"
      Dim regex As New Regex("")
      Dim substrings() As String = regex.Split(input)
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write(substrings(ctr))
         If ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example produces the following output:   
'    {, c, h, a, r, a, c, t, e, r, s, }

返される配列には、配列の先頭と末尾に空の文字列も含まれていることに注意してください。

分割操作の実行時間が、Regex.Regex(String, RegexOptions, TimeSpan) コンストラクターで指定されたタイムアウト間隔を超えると、RegexMatchTimeoutException 例外がスローされます。 コンストラクターを呼び出すときにタイムアウト間隔を設定しない場合、Regex オブジェクトが作成されるアプリケーション ドメインに対して確立されたタイムアウト値を操作が超えた場合、例外がスローされます。 Regex コンストラクター呼び出しまたはアプリケーション ドメインのプロパティでタイムアウトが定義されていない場合、またはタイムアウト値が Regex.InfiniteMatchTimeout場合、例外はスローされません

こちらもご覧ください

適用対象

Split(String, Int32)

ソース:
Regex.Split.cs
ソース:
Regex.Split.cs
ソース:
Regex.Split.cs

指定した最大回数の入力文字列を、Regex コンストラクターで指定された正規表現によって定義された位置にある部分文字列の配列に分割します。

public:
 cli::array <System::String ^> ^ Split(System::String ^ input, int count);
public string[] Split (string input, int count);
member this.Split : string * int -> string[]
Public Function Split (input As String, count As Integer) As String()

パラメーター

input
String

分割する文字列。

count
Int32

分割を実行できる最大回数。

戻り値

String[]

文字列の配列。

例外

inputnullです。

タイムアウトが発生しました。 タイムアウトの詳細については、「解説」セクションを参照してください。

注釈

Regex.Split メソッドは、String.Split メソッドに似ていますが、Regex.Split は、文字列を一連の文字ではなく正規表現によって決定された区切り記号で分割する点が異なります。 count パラメーターは、input 文字列を分割できる部分文字列の最大数を指定します。最後の文字列には、文字列の分割されていない剰余が含まれています。 count の値が 0 の場合、可能な限り多くの時間を分割する既定の動作が提供されます。

複数の一致が互いに隣接している場合、または inputの先頭または末尾に一致が見つかり、検出された一致の数が countより少なくとも 2 つ少ない場合は、空の文字列が配列に挿入されます。 つまり、隣接する一致または入力文字列の先頭または末尾の一致に起因する空の文字列は、一致する部分文字列の数が count等しいかどうかを判断する際にカウントされます。 次の例では、正規表現 /d+ を使用して、1 つ以上の 10 進数を含む入力文字列を最大 3 つの部分文字列に分割します。 入力文字列の先頭は正規表現パターンと一致するため、最初の配列要素には String.Emptyが含まれており、2 番目の配列要素には入力文字列の最初の英字セットが含まれており、3 番目の配列要素には 3 番目の一致に続く文字列の残りの部分が含まれます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      
      string[] result = rgx.Split(input, 3);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL789MNOPQ012'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim rgx As New Regex(pattern)
      Dim input As String = "123ABCDE456FGHIJKL789MNOPQ012"
      
      Dim result() As String = rgx.Split(input, 3)
      For ctr As Integer = 0 To result.Length - 1
         Console.Write("'{0}'", result(ctr))
         If ctr < result.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine()
   End Sub               
End Module
' The example displays the following output:
'       '', 'ABCDE', 'FGHIJKL789MNOPQ012'

かっこのキャプチャが正規表現で使用されている場合、キャプチャされたテキストは分割文字列の配列に含まれます。 ただし、キャプチャされたテキストを含む配列要素は、一致する数が countに達したかどうかを判断する際にはカウントされません。 たとえば、次のコードに示すように、文字列 "apple-apricot-plum-pear-banana" を最大 4 つの部分文字列に分割すると、7 要素配列になります。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)";
      string input = "apple-apricot-plum-pear-banana";
      Regex regex = new Regex(pattern);         // Split on hyphens.
      string[] substrings = regex.Split(input, 4);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//       'apple'
//       '-'
//       'apricot'
//       '-'
//       'plum'
//       '-'
//       'pear-banana'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(-)"
      Dim input As String = "apple-apricot-plum-pear-banana"
      Dim regex As Regex = New Regex(pattern)         ' Split on hyphens.
      Dim substrings() As String = regex.Split(input, 4)
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The example displays the following output:
'    'apple'
'    '-'
'    'apricot'
'    '-'
'    'plum'
'    '-'
'    'pear-banana'

ただし、正規表現パターンに複数のキャプチャかっこセットが含まれている場合、このメソッドの動作は .NET Framework のバージョンによって異なります。 .NET Framework 1.0 および 1.1 では、キャプチャかっこの最初のセットからキャプチャされたテキストのみが、返される配列に含まれます。 .NET Framework 2.0 以降では、キャプチャされたすべてのテキストが返された配列に追加されます。 ただし、キャプチャされたテキストを含む返される配列内の要素は、一致する部分文字列の数が count等しいかどうかを判断する際にはカウントされません。 たとえば、次のコードでは、正規表現で 2 組のキャプチャかっこを使用して、日付文字列から日付の要素を抽出します。 かっこをキャプチャする最初のセットはハイフンをキャプチャし、2 番目のセットはスラッシュをキャプチャします。 Split(String, Int32) メソッドの呼び出しでは、返される配列内の最大 2 つの要素を指定します。 コード例がコンパイルされ、.NET Framework 1.0 または 1.1 で実行されている場合、メソッドは 2 要素の文字列配列を返します。 コンパイルされ、.NET Framework 2.0 以降のバージョンで実行されている場合、このメソッドは 3 要素の文字列配列を返します。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";
      Regex regex = new Regex(pattern);
      foreach (string result in regex.Split(input, 2)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// Under .NET 1.0 and 1.1, the method returns an array of
// 2 elements, as follows:
//    '07'
//    '14/2007'
//
// Under .NET 2.0 and later, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '/'
//    '14/2007'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "07/14/2007"   
      Dim pattern As String = "(-)|(/)"
      Dim regex As Regex = New Regex(pattern)
      For Each result As String In regex.Split(input, 2) 
         Console.WriteLine("'{0}'", result)
      Next
   End Sub
End Module
' In .NET 1.0 and 1.1, the method returns an array of
' 2 elements, as follows:
'    '07'
'    '14/2007'
'
' In .NET 2.0 and later, the method returns an array of
' 3 elements, as follows:
'    '07'
'    '/'
'    '14/2007'

正規表現が空の文字列と一致する場合、Split(String, Int32) は、空の文字列区切り記号がすべての場所で見つかるので、文字列を 1 文字の文字列の配列に分割します。 次の例では、文字列 "characters" を入力文字列内の要素と同じ数の要素に分割します。 null 文字列は入力文字列の先頭と一致するため、返された配列の先頭に null 文字列が挿入されます。 これにより、10 番目の要素は、入力文字列の末尾にある 2 つの文字で構成されます。

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "characters"
      Dim regex As New Regex("")
      Dim substrings() As String = regex.Split(input, input.Length)
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write(substrings(ctr))
         if ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example displays the following output:   
'    {, c, h, a, r, a, c, t, e, rs}

分割操作の実行時間が、Regex.Regex(String, RegexOptions, TimeSpan) コンストラクターで指定されたタイムアウト間隔を超えると、RegexMatchTimeoutException 例外がスローされます。 コンストラクターを呼び出すときにタイムアウト間隔を設定しない場合、Regex オブジェクトが作成されるアプリケーション ドメインに対して確立されたタイムアウト値を操作が超えた場合、例外がスローされます。 Regex コンストラクター呼び出しまたはアプリケーション ドメインのプロパティでタイムアウトが定義されていない場合、またはタイムアウト値が Regex.InfiniteMatchTimeout場合、例外はスローされません

こちらもご覧ください

適用対象