Regex.Split 方法

定義

在規則運算式比對所定義的位置,將輸入字串分割成子字串陣列。

多載

Split(String)

Regex 建構函式中指定之規則運算式模式所定義的位置,將輸入字串分隔成子字串的陣列。

Split(String, Int32)

Regex 建構函式中指定的規則運算式所定義的位置,以指定的最大次數來將輸入字串分隔成子字串的陣列。

Split(String, String)

在規則運算式模式所定義的位置,將輸入字串分割成子字串陣列。

Split(String, Int32, Int32)

Regex 建構函式中指定的規則運算式所定義的位置,以指定的最大次數來將輸入字串分隔成子字串的陣列。 規則運算式模式從輸入字串中指定的字元位置開始搜尋。

Split(String, String, RegexOptions)

在指定的規則運算式模式所定義的位置,將輸入字串分割成子字串陣列。 指定的選項會修改符合的作業。

Split(String, String, RegexOptions, TimeSpan)

在指定的規則運算式模式所定義的位置,將輸入字串分割成子字串陣列。 如果沒有找到相符項目,其他參數會指定修改比對作業的選項和逾時間隔。

Split(String)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
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 分割正則運算式所決定之分隔符號的字串,而不是一組字元。 字串會盡可能分割多次。 如果找不到分隔符號,則傳回值會包含一個元素,其值為原始輸入字串。

如果多個相符專案彼此相鄰,則會將空字串插入陣列中。 例如,在單一連字號上分割字串會導致傳回的陣列在找到兩個相鄰連字號的位置中包含空字串,如下列程式碼所示。

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 開始,所有擷取的文字也會新增至傳回的陣列。 例如,下列程式碼會使用兩組擷取括弧,從日期字串中擷取日期元素,包括日期分隔符號。 第一組擷取括弧會擷取連字號,而第二組則會擷取正斜線。 如果範例程式碼經過編譯,並在 .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) 因為可以在每個位置找到空字串分隔符號。 例如:

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, }

請注意,傳回的陣列也會在陣列的開頭和結尾包含空字串。

RegexMatchTimeoutException如果分割作業的執行時間超過建構函式指定的 Regex.Regex(String, RegexOptions, TimeSpan) 逾時間隔,就會擲回例外狀況。 呼叫建構函式時若未設定逾時間隔,則如果作業超過為建立 Regex 物件的應用程式定義域設定的任何逾時值,就會擲回例外狀況。 如果在 Regex 建構函式呼叫或應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況

另請參閱

適用於

Split(String, Int32)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
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值為零會盡可能提供分割次數的預設行為。

如果多個相符專案彼此相鄰,或在 的開頭或結尾 input 找到相符專案,且找到的相符專案數目至少小於 count 兩個,則會將空字串插入陣列中。 也就是說,在判斷相符子字串數目是否等於 count 時,會計算來自相鄰相符專案或輸入字串開頭或結尾的相符專案所產生的空字串。 在下列範例中,正則運算式 /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, 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」 分割成最多四個子字串會導致七個專案陣列,如下列程式碼所示。

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 時,不會計算包含所擷取文字之傳回陣列中的元素。 例如,在下列程式碼中,正則運算式會使用兩組擷取括弧,從日期字串擷取日期的專案。 第一組擷取括弧會擷取連字號,而第二組則會擷取正斜線。 接著,呼叫 Split(String, Int32) 方法會指定傳回陣列中最多兩個專案。 如果範例程式碼經過編譯,並在 .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, 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) 因為可以在每個位置找到空字串分隔符號。 下列範例會將字串 「characters」 分割成輸入字串中有多少個元素。 因為 Null 字串符合輸入字串的開頭,所以會在傳回陣列的開頭插入 Null 字串。 這會導致第十個元素包含輸入字串結尾的兩個字元。

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}

RegexMatchTimeoutException如果分割作業的執行時間超過建構函式指定的 Regex.Regex(String, RegexOptions, TimeSpan) 逾時間隔,就會擲回例外狀況。 呼叫建構函式時若未設定逾時間隔,則如果作業超過為建立 Regex 物件的應用程式定義域設定的任何逾時值,就會擲回例外狀況。 如果在 Regex 建構函式呼叫或應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況

另請參閱

適用於

Split(String, String)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
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[]

字串的陣列。

例外狀況

發生規則運算式剖析錯誤。

inputpatternnull

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

備註

方法 Regex.Split 類似于 String.Split 方法,不同之處在于 Regex.Split 分割正則運算式所決定之分隔符號的字串,而不是一組字元。 字串 input 會盡可能分割多次。 如果在 pattern 字串中 input 找不到 ,則傳回值會包含一個專案,其值為原始 input 字串。

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。 如需正則運算式的詳細資訊,請參閱 .NET 正則運算式正則運算式語言 - 快速參考

重要

自動快取用於呼叫靜態 Split 方法的已編譯正則運算式。 若要自行管理已編譯正則運算式的存留期,請使用 實例 Split 方法。

如果多個相符專案彼此相鄰,則會將空字串插入陣列中。 例如,在單一連字號上分割字串會導致傳回的陣列在找到兩個相鄰連字號的位置中包含空字串,如下列程式碼所示。

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 開始,所有擷取的文字也會新增至傳回的陣列。 例如,下列程式碼會使用兩組擷取括弧,從日期字串中擷取日期元素,包括日期分隔符號。 第一組擷取括弧會擷取連字號,而第二組則會擷取正斜線。 如果範例程式碼經過編譯,並在 .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 因為可以在每個位置找到空字串分隔符號。 例如:

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, Int32, Int32)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
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 小於零或大於 input 的長度。

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

備註

方法 Regex.Split 類似于 String.Split 方法,不同之處在于 Regex.Split 分割正則運算式所決定之分隔符號的字串,而不是一組字元。 參數 count 會指定要分割字串的子字串 input 數目上限;最後一個字串包含字串的未分隔餘數。 count值為零會盡可能提供分割次數的預設行為。 參數 startat 會定義第一個分隔符號的搜尋開始點, (這可用於略過前置空白字元) 。

如需 的詳細資訊 startat ,請參閱 的一 Match(String, Int32) 節。

如果在字串中的 +1 位置找不到 count 相符專案,方法會傳回包含字串的 input 一個專案陣列。 如果找到一或多個相符專案,傳回陣列的第一個元素會包含字串的第一個部分,最多一個字元之後才相符。

如果多個相符專案彼此相鄰,且找到的相符專案數目至少小於 count 兩個 ,則會將空字串插入陣列中。 同樣地,如果在 找到 startat 相符專案,這是字串中的第一個字元,則傳回陣列的第一個專案是空字串。 也就是說,判斷相符子字串數目是否等於 count 時,會計算來自相鄰相符專案的空字串數目。 在下列範例中,正則運算式 \d+ 是用來尋找字串中第一個數值字元子字串的開始位置,然後從該位置開始,將字串分割最多三次。 因為正則運算式模式符合輸入字串的開頭,所以傳回的字串陣列包含空字串、五個字元字母字串,以及字串的其餘部分。

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 開始會產生七個專案陣列,如下列程式碼所示。

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 開始,所有擷取的文字也會新增至傳回的陣列。 例如,下列程式碼會使用兩組擷取括弧來擷取字串中的個別單字。 第一組擷取括弧會擷取連字號,而第二組則會擷取垂直線。 如果範例程式碼經過編譯,並在 .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 因為可以在每個位置找到空字串分隔符號。 下列範例會從字元 「a」 開始,將字串 「characters」 分割成輸入字串所包含的專案數目。 因為 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}

RegexMatchTimeoutException如果分割作業的執行時間超過建構函式指定的 Regex.Regex(String, RegexOptions, TimeSpan) 逾時間隔,就會擲回例外狀況。 呼叫建構函式時若未設定逾時間隔,則如果作業超過為建立 Regex 物件的應用程式定義域設定的任何逾時值,就會擲回例外狀況。 如果在 Regex 建構函式呼叫或應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況

另請參閱

適用於

Split(String, String, RegexOptions)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
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[]

字串的陣列。

例外狀況

發生規則運算式剖析錯誤。

inputpatternnull

options 不是 RegexOptions 值的有效位元組合。

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

備註

方法 Regex.Split 類似于 String.Split(Char[]) 方法,不同之處在于 Regex.Split 分割正則運算式所決定之分隔符號的字串,而不是一組字元。 字串會盡可能分割多次。 如果找不到分隔符號,則傳回值會包含一個專案,其值為原始 input 字串。

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。 如需正則運算式的詳細資訊,請參閱 .NET 正則運算式正則運算式語言 - 快速參考

重要

自動快取用於呼叫靜態 Split 方法的已編譯正則運算式。 若要自行管理已編譯正則運算式的存留期,請使用 實例 Split 方法。

如果多個相符專案彼此相鄰,則會將空字串插入陣列中。 例如,在單一連字號上分割字串會導致傳回的陣列在找到兩個相鄰連字號的位置中包含空字串。

如果在輸入字串的開頭或結尾找到相符專案,則會在傳回陣列的開頭或結尾包含空字串。 下列範例會使用正則運算式模式 [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 開始,所有擷取的文字也會新增至傳回的陣列。 例如,下列程式碼會使用兩組擷取括弧,從日期字串中擷取日期元素,包括日期分隔符號。 第一組擷取括弧會擷取連字號,而第二組則會擷取正斜線。 如果範例程式碼經過編譯,並在 .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 因為可以在每個位置找到空字串分隔符號。

RegexMatchTimeoutException如果分割作業的執行時間超過呼叫方法的應用程式域所指定的逾時間隔,就會擲回例外狀況。 如果在應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況。

給呼叫者的注意事項

這個方法會在等於呼叫方法之應用程式域的預設逾時值間隔之後逾時。 如果未針對應用程式定義域定義逾時值,則使用值 InfiniteMatchTimeout,使方法不會逾時。 在模式比對上分割文字的建議靜態方法為 Split(String, String, RegexOptions, TimeSpan) ,可讓您設定逾時間隔。

另請參閱

適用於

Split(String, String, RegexOptions, TimeSpan)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
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[]

字串陣列。

例外狀況

發生規則運算式剖析錯誤。

inputpatternnull

options 不是 RegexOptions 值的有效位元組合。

-或-

matchTimeout 為負數、零或約大於 24 天。

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

備註

方法 Regex.Split 與 方法類似 String.Split(Char[]) ,不同之處在于 Regex.Split 將字串分割為正則運算式所決定的分隔符號,而不是一組字元。 字串會盡可能分割多次。 如果找不到分隔符號,傳回值會包含一個專案,其值為原始 input 字串。

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。 如需正則運算式的詳細資訊,請參閱 .NET 正則運算式正則運算式語言 - 快速參考

重要

系統會自動快取用於呼叫靜態 Split 方法的已編譯正則運算式。 若要自行管理已編譯正則運算式的存留期,請使用 實例 Split 方法。

如果多個相符專案彼此相鄰,則會將空字串插入陣列中。 例如,在單一連字號上分割字串會導致傳回的陣列在找到兩個相鄰連字號的位置中包含空字串。

如果在輸入字串的開頭或結尾找到相符專案,則會在傳回陣列的開頭或結尾包含空字串。 下列範例會使用正則運算式模式 [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 開始,所有擷取的文字也會新增至傳回的陣列。 例如,下列程式碼會使用兩組擷取括弧,從日期字串擷取日期的專案,包括日期分隔符號。 第一組擷取括弧會擷取連字號,而第二組則會擷取正斜線。 如果編譯範例程式碼並在 .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 因為可以在每個位置找到空字串分隔符號。

參數 matchTimeout 會指定模式比對方法在逾時之前應該嘗試尋找相符專案的時間長度。設定逾時間隔可防止依賴過多回溯的正則運算式在處理包含接近相符專案的輸入時停止回應。 如需詳細資訊,請參閱正則運算式和回溯的最佳做法。 如果該時間間隔中找不到相符專案,方法會 RegexMatchTimeoutException 擲回例外狀況。 matchTimeout 會覆寫針對方法執行所在應用程式域所定義的任何預設逾時值。

給呼叫者的注意事項

我們建議您將 matchTimeout 參數設定為適當的值,例如兩秒。 如果您藉由指定 InfiniteMatchTimeout 來停用逾時,正則運算式引擎會提供稍微更好的效能。 不過,您應該只在下列情況下停用逾時:

  • 當正則運算式處理的輸入衍生自已知且受信任的來源,或由靜態文字所組成時。 這不包括使用者動態輸入的文字。

  • 當正則運算式模式經過徹底測試,以確保其有效率地處理相符專案、非相符專案和接近相符專案。

  • 當正則運算式模式不包含已知在處理接近相符專案時造成過多回溯的語言專案時。

另請參閱

適用於