Regex.Split 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将输入字符串拆分为正则表达式匹配所定义位置的子字符串数组。
重载
| 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)
- 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 指示方法不应超时。
返回
字符串数组。
例外
发生正则表达式分析错误。
              input 或 patternnull。
发生超时。 有关超时的详细信息,请参阅“备注”部分。
注解
              Regex.Split 方法类似于 String.Split(Char[]) 方法,不同之处在于,Regex.Split 在正则表达式而不是一组字符确定的分隔符处拆分字符串。 字符串会尽可能多地拆分。 如果未找到分隔符,则返回值包含一个元素,其值是原始 input 字符串。
              pattern 参数由正则表达式语言元素组成,这些元素以符号方式描述要匹配的字符串。 有关正则表达式的详细信息,请参阅 .NET 正则表达式 和 正则表达式语言 - 快速参考。
如果多个匹配项彼此相邻,则会将空字符串插入数组中。 例如,在单个连字符上拆分字符串会导致返回的数组在找到两个相邻连字符的位置中包含空字符串。
如果在输入字符串的开头或末尾找到匹配项,则返回的数组的开头或末尾将包含一个空字符串。 以下示例使用正则表达式模式 [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来禁用超时,则正则表达式引擎提供略微更好的性能。 但是,应仅在以下情况下禁用超时:
- 当正则表达式处理的输入派生自已知且受信任的源或包含静态文本时。 这不包括用户动态输入的文本。 
- 当正则表达式模式经过全面测试以确保它有效地处理匹配项、非匹配项和接近匹配项时。 
- 当正则表达式模式不包含任何已知在处理接近匹配时导致过度回溯的语言元素。 
另请参阅
适用于
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
提供匹配选项的枚举值的按位组合。
返回
字符串数组。
例外
发生正则表达式分析错误。
              input 或 patternnull。
              options 不是 RegexOptions 值的有效按位组合。
发生超时。 有关超时的详细信息,请参阅“备注”部分。
注解
              Regex.Split 方法类似于 String.Split(Char[]) 方法,不同之处在于,Regex.Split 在正则表达式而不是一组字符确定的分隔符处拆分字符串。 字符串会尽可能多地拆分。 如果未找到分隔符,则返回值包含一个元素,其值是原始 input 字符串。
              pattern 参数由正则表达式语言元素组成,这些元素以符号方式描述要匹配的字符串。 有关正则表达式的详细信息,请参阅 .NET 正则表达式 和 正则表达式语言 - 快速参考。
如果多个匹配项彼此相邻,则会将空字符串插入数组中。 例如,在单个连字符上拆分字符串会导致返回的数组在找到两个相邻连字符的位置中包含空字符串。
如果在输入字符串的开头或末尾找到匹配项,则返回的数组的开头或末尾将包含一个空字符串。 以下示例使用正则表达式模式 [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, 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
将在其中开始搜索的输入字符串中的字符位置。
返回
字符串数组。
例外
              input
              null。
              startat 小于零或大于 input的长度。
发生超时。 有关超时的详细信息,请参阅“备注”部分。
注解
              Regex.Split 方法类似于 String.Split 方法,不同之处在于,Regex.Split 在正则表达式而不是一组字符确定的分隔符处拆分字符串。 
              count 参数指定拆分 input 字符串的最大子字符串数;最后一个字符串包含字符串的未指定剩余部分。 
              count 值为零,提供尽可能多地拆分的默认行为。 
              startat 参数定义第一个分隔符开始搜索的点(这可用于跳过前导空格)。
有关 startat的更多详细信息,请参阅 Match(String, Int32)的“备注”部分。
如果在字符串中 count+1 位置找不到匹配项,该方法将返回一个包含 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-pineapple-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 会将字符串拆分为单个字符字符串数组,因为空字符串分隔符可以在每个位置找到。 下面的示例将字符串“characters”拆分为输入字符串包含的任意数量的元素,以字符“a”开头。 由于 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)
- 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
要匹配的正则表达式模式。
返回
字符串数组。
例外
发生正则表达式分析错误。
              input 或 patternnull。
发生超时。 有关超时的详细信息,请参阅“备注”部分。
注解
              Regex.Split 方法类似于 String.Split 方法,不同之处在于,Regex.Split 在正则表达式而不是一组字符确定的分隔符处拆分字符串。 
              input 字符串会尽可能多地拆分。 如果在 input 字符串中找不到 pattern,则返回值包含一个元素,其值为原始 input 字符串。
              pattern 参数由正则表达式语言元素组成,这些元素以符号方式描述要匹配的字符串。 有关正则表达式的详细信息,请参阅 .NET 正则表达式 和 正则表达式语言 - 快速参考。
如果多个匹配项彼此相邻,则会将空字符串插入数组中。 例如,在单个连字符上拆分字符串会导致返回的数组在找到两个相邻连字符的位置中包含空字符串,如以下代码所示。
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)
- 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
要拆分的字符串。
返回
字符串数组。
例外
              input
              null。
发生超时。 有关超时的详细信息,请参阅“备注”部分。
注解
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, }
请注意,返回的数组还包括数组开头和末尾的空字符串。
如果拆分操作的执行时间超过 Regex.Regex(String, RegexOptions, TimeSpan) 构造函数指定的超时间隔,则会引发 RegexMatchTimeoutException 异常。 如果在调用构造函数时未设置超时间隔,则如果操作超出了为在其中创建 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
拆分可以发生的最大次数。
返回
字符串数组。
例外
              input
              null。
发生超时。 有关超时的详细信息,请参阅“备注”部分。
注解
              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}
如果拆分操作的执行时间超过 Regex.Regex(String, RegexOptions, TimeSpan) 构造函数指定的超时间隔,则会引发 RegexMatchTimeoutException 异常。 如果在调用构造函数时未设置超时间隔,则如果操作超出了为在其中创建 Regex 对象的应用程序域建立的任何超时值,则会引发异常。 如果在 Regex 构造函数调用或应用程序域的属性中未定义超时,或者超时值 Regex.InfiniteMatchTimeout,则不会引发异常