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)“备注”部分。

如果未从字符串中的 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-p石榴-菠萝-桃子”拆分为字符串中从字符 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 字符串与输入字符串的末尾匹配,因此在返回的数组的末尾插入一个 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 适当的值,例如 2 秒。 如果通过指定 InfiniteMatchTimeout来禁用超时,则正则表达式引擎提供的性能稍好一些。 但是,应仅在以下情况下禁用超时:

  • 当正则表达式处理的输入派生自已知且受信任的源或由静态文本组成时。 这不包括用户动态输入的文本。

  • 当正则表达式模式经过全面测试以确保它有效地处理匹配项、非匹配项和接近匹配项时。

  • 当正则表达式模式不包含已知在处理接近匹配时导致过度回溯的语言元素时。

另请参阅

适用于