Regex.Split 方法

定义

将一个输入字符串拆分为位于正则表达式匹配定义的位置的子字符串的数组。Splits an input string into an array of substrings at the positions defined by a regular expression match.

重载

Split(String, String, RegexOptions, TimeSpan)

在由指定正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。Splits an input string into an array of substrings at the positions defined by a specified regular expression pattern. 如果未找到匹配项,则其他参数指定修改匹配操作的选项和超时间隔。Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

Split(String, String, RegexOptions)

在由指定正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。Splits an input string into an array of substrings at the positions defined by a specified regular expression pattern. 指定的选项将修改匹配操作。Specified options modify the matching operation.

Split(String, Int32, Int32)

在由 Regex 构造函数中指定的正则表达式定义的位置,将输入字符串拆分为子字符串数组指定的最大次数。Splits an input string a specified maximum number of times into an array of substrings, at the positions defined by a regular expression specified in the Regex constructor. 从输入字符串的指定字符位置开始搜索正则表达式模式。The search for the regular expression pattern starts at a specified character position in the input string.

Split(String, Int32)

在由 Regex 构造函数中指定的正则表达式定义的位置,将输入字符串拆分为子字符串数组指定的最大次数。Splits an input string a specified maximum number of times into an array of substrings, at the positions defined by a regular expression specified in the Regex constructor.

Split(String)

在由 Regex 构造函数指定的正则表达式模式所定义的位置,将输入字符串拆分为子字符串数组。Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.

Split(String, String)

在由正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。Splits an input string into an array of substrings at the positions defined by a regular expression pattern.

Split(String, String, RegexOptions, TimeSpan)

在由指定正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。Splits an input string into an array of substrings at the positions defined by a specified regular expression pattern. 如果未找到匹配项,则其他参数指定修改匹配操作的选项和超时间隔。Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

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

要拆分的字符串。The string to split.

pattern
String

要匹配的正则表达式模式。The regular expression pattern to match.

options
RegexOptions

枚举值的一个按位组合,这些枚举值提供匹配选项。A bitwise combination of the enumeration values that provide options for matching.

matchTimeout
TimeSpan

超时间隔;若要指示该方法不应超时,则为 InfiniteMatchTimeoutA time-out interval, or InfiniteMatchTimeout to indicate that the method should not time out.

返回

String[]

字符串数组。A string array.

异常

出现正则表达式分析错误。A regular expression parsing error occurred.

inputpatternnullinput or pattern is null.

options 不是 RegexOptions 值的有效按位组合。options is not a valid bitwise combination of RegexOptions values.

- 或 --or- matchTimeout 为负、零或大于 24 天左右。matchTimeout is negative, zero, or greater than approximately 24 days.

发生超时。A time-out occurred. 有关超时的详细信息,请参阅“备注”部分。For more information about time-outs, see the Remarks section.

注解

Regex.Split 方法与 String.Split(Char[]) 方法类似,不同之处在于 Regex.Split 在由正则表达式(而不是一组字符)确定的分隔符处拆分字符串。The Regex.Split methods are similar to the String.Split(Char[]) method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. 尽可能多地拆分字符串。The string is split as many times as possible. 如果未找到分隔符,则返回值将包含一个值为原始 input 字符串的元素。If no delimiter is found, the return value contains one element whose value is the original input string.

pattern 参数包含符号描述要匹配的字符串的正则表达式语言元素。The pattern parameter consists of regular expression language elements that symbolically describe the string to match. 有关正则表达式的详细信息,请参阅.NET Framework 正则表达式正则表达式语言-快速参考For more information about regular expressions, see .NET Framework Regular Expressions and Regular Expression Language - Quick Reference.

重要

对静态 Split 方法的调用中使用的已编译正则表达式会自动缓存。Compiled regular expressions used in calls to static Split methods are automatically cached. 若要自行管理已编译的正则表达式的生存期,请使用实例 Split 方法。To manage the lifetime of compiled regular expressions yourself, use the instance Split methods.

如果多个匹配项彼此相邻,则将空字符串插入到数组中。If multiple matches are adjacent to one another, an empty string is inserted into the array. 例如,将字符串拆分为单个连字符会导致返回的数组在找到两个相邻连字符的位置包含空字符串。For example, splitting a string on a single hyphen causes the returned array to include an empty string in the position where two adjacent hyphens are found.

如果在输入字符串的开头或末尾找到了匹配项,则返回数组的开头或结尾处将包含空字符串。If a match is found at the beginning or the end of the input string, an empty string is included at the beginning or the end of the returned array. 下面的示例使用正则表达式模式 [a-z]+ 在任何大写或小写字母字符上拆分输入字符串。The following example uses the regular expression pattern [a-z]+ to split an input string on any uppercase or lowercase alphabetic character. 因为字符串以匹配的字母字符开头和结尾,所以返回数组的第一个和最后一个元素的值是 String.EmptyBecause the string begins and ends with matching alphabetic characters, the value of the first and last element of the returned array is 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 表达式中使用捕获括号,则生成的字符串数组中将包括任何捕获的文本。If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 例如,如果在放在捕获括号内的连字符处拆分字符串 "plum",则返回的数组包含一个包含连字符的字符串元素。For example, if you split the string "plum-pear" on a hyphen placed within capturing parentheses, the returned array includes a string element that contains the hyphen.

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 的版本。However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. 在 .NET Framework 1.0 和1.1 中,如果在第一组捕获括号中找不到匹配项,则不会在返回的数组中包含从其他捕获括号中捕获的文本。In the .NET Framework 1.0 and 1.1, if a match is not found within the first set of capturing parentheses, captured text from additional capturing parentheses is not included in the returned array. 从 .NET Framework 2.0 开始,所有捕获的文本也会添加到返回的数组中。Starting with the .NET Framework 2.0, all captured text is also added to the returned array. 例如,下面的代码使用两组捕获括号从日期字符串提取日期的元素,包括日期分隔符。For example, the following code uses two sets of capturing parentheses to extract the elements of a date, including the date delimiters, from a date string. 第一组捕获括号捕获连字符,第二组捕获的是正斜杠。The first set of capturing parentheses captures the hyphen, and the second set captures the forward slash. 如果在 .NET Framework 1.0 或1.1 下编译并运行示例代码,它将排除斜杠字符;如果编译并在 .NET Framework 2.0 或更高版本下运行,则它将包含它们。If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the slash characters; if it is compiled and run under the .NET Framework 2.0 or later versions, it includes them.

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 会将字符串拆分为单字符字符串数组,因为在每个位置都可以找到空字符串分隔符。If the regular expression can match the empty string, Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location.

matchTimeout 参数指定模式匹配方法在超时之前应尝试找到匹配项的时间长度。设置超时间隔可防止正则表达式在处理包含接近匹配项的输入时,这些表达式会出现依赖于过量回溯的正则表达式停止响应。The matchTimeout parameter specifies how long a pattern matching method should try to find a match before it times out. Setting a time-out interval prevents regular expressions that rely on excessive backtracking from appearing to stop responding when they process input that contains near matches. 有关详细信息,请参阅正则表达式和回溯的最佳实践For more information, see Best Practices for Regular Expressions and Backtracking. 如果在该时间间隔内未找到匹配项,则该方法将引发 RegexMatchTimeoutException 异常。If no match is found in that time interval, the method throws a RegexMatchTimeoutException exception. matchTimeout 覆盖为在其中执行该方法的应用程序域定义的任何默认超时值。matchTimeout overrides any default time-out value defined for the application domain in which the method executes.

调用方说明

建议将 matchTimeout 参数设置为适当的值,例如两秒钟。We recommend that you set the matchTimeout parameter to an appropriate value, such as two seconds. 如果通过指定 InfiniteMatchTimeout禁用超时,则正则表达式引擎可提供略微更好的性能。If you disable time-outs by specifying InfiniteMatchTimeout, the regular expression engine offers slightly better performance. 但是,只应在以下条件下禁用超时:However, you should disable time-outs only under the following conditions: -正则表达式处理的输入派生自已知的和受信任的源,或由静态文本组成。- When the input processed by a regular expression is derived from a known and trusted source or consists of static text. 这不包括用户动态输入的文本。This excludes text that has been dynamically input by users.

-对正则表达式模式进行了全面测试,以确保它有效地处理匹配、不匹配和接近匹配项。- When the regular expression pattern has been thoroughly tested to ensure that it efficiently handles matches, non-matches, and near matches.

-当正则表达式模式不包含已知的语言元素时,这些元素会导致在处理接近的匹配时导致过度回溯。- When the regular expression pattern contains no language elements that are known to cause excessive backtracking when processing a near match.

另请参阅

Split(String, String, RegexOptions)

在由指定正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。Splits an input string into an array of substrings at the positions defined by a specified regular expression pattern. 指定的选项将修改匹配操作。Specified options modify the matching operation.

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

要拆分的字符串。The string to split.

pattern
String

要匹配的正则表达式模式。The regular expression pattern to match.

options
RegexOptions

枚举值的一个按位组合,这些枚举值提供匹配选项。A bitwise combination of the enumeration values that provide options for matching.

返回

String[]

字符串数组。An array of strings.

异常

出现正则表达式分析错误。A regular expression parsing error occurred.

inputpatternnullinput or pattern is null.

options 不是 RegexOptions 值的有效按位组合。options is not a valid bitwise combination of RegexOptions values.

发生超时。A time-out occurred. 有关超时的详细信息,请参阅“备注”部分。For more information about time-outs, see the Remarks section.

注解

Regex.Split 方法与 String.Split(Char[]) 方法类似,不同之处在于 Regex.Split 在由正则表达式(而不是一组字符)确定的分隔符处拆分字符串。The Regex.Split methods are similar to the String.Split(Char[]) method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. 尽可能多地拆分字符串。The string is split as many times as possible. 如果未找到分隔符,则返回值将包含一个值为原始 input 字符串的元素。If no delimiter is found, the return value contains one element whose value is the original input string.

pattern 参数包含符号描述要匹配的字符串的正则表达式语言元素。The pattern parameter consists of regular expression language elements that symbolically describe the string to match. 有关正则表达式的详细信息,请参阅.NET Framework 正则表达式正则表达式语言-快速参考For more information about regular expressions, see .NET Framework Regular Expressions and Regular Expression Language - Quick Reference.

重要

对静态 Split 方法的调用中使用的已编译正则表达式会自动缓存。Compiled regular expressions used in calls to static Split methods are automatically cached. 若要自行管理已编译的正则表达式的生存期,请使用实例 Split 方法。To manage the lifetime of compiled regular expressions yourself, use the instance Split methods.

如果多个匹配项彼此相邻,则将空字符串插入到数组中。If multiple matches are adjacent to one another, an empty string is inserted into the array. 例如,将字符串拆分为单个连字符会导致返回的数组在找到两个相邻连字符的位置包含空字符串。For example, splitting a string on a single hyphen causes the returned array to include an empty string in the position where two adjacent hyphens are found.

如果在输入字符串的开头或末尾找到了匹配项,则返回数组的开头或结尾处将包含空字符串。If a match is found at the beginning or the end of the input string, an empty string is included at the beginning or the end of the returned array. 下面的示例使用正则表达式模式 [a-z]+ 在任何大写或小写字母字符上拆分输入字符串。The following example uses the regular expression pattern [a-z]+ to split an input string on any uppercase or lowercase alphabetic character. 因为字符串以匹配的字母字符开头和结尾,所以返回数组的第一个和最后一个元素的值是 String.EmptyBecause the string begins and ends with matching alphabetic characters, the value of the first and last element of the returned array is 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 表达式中使用捕获括号,则生成的字符串数组中将包括任何捕获的文本。If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 例如,如果在放在捕获括号内的连字符处拆分字符串 "plum",则返回的数组包含一个包含连字符的字符串元素。For example, if you split the string "plum-pear" on a hyphen placed within capturing parentheses, the returned array includes a string element that contains the hyphen.

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 的版本。However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. 在 .NET Framework 1.0 和1.1 中,如果在第一组捕获括号中找不到匹配项,则不会在返回的数组中包含从其他捕获括号中捕获的文本。In the .NET Framework 1.0 and 1.1, if a match is not found within the first set of capturing parentheses, captured text from additional capturing parentheses is not included in the returned array. 从 .NET Framework 2.0 开始,所有捕获的文本也会添加到返回的数组中。Starting with the .NET Framework 2.0, all captured text is also added to the returned array. 例如,下面的代码使用两组捕获括号从日期字符串提取日期的元素,包括日期分隔符。For example, the following code uses two sets of capturing parentheses to extract the elements of a date, including the date delimiters, from a date string. 第一组捕获括号捕获连字符,第二组捕获的是正斜杠。The first set of capturing parentheses captures the hyphen, and the second set captures the forward slash. 如果在 .NET Framework 1.0 或1.1 下编译并运行示例代码,它将排除斜杠字符;如果编译并在 .NET Framework 2.0 或更高版本下运行,则它将包含它们。If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the slash characters; if it is compiled and run under the .NET Framework 2.0 or later versions, it includes them.

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 会将字符串拆分为单字符字符串数组,因为在每个位置都可以找到空字符串分隔符。If the regular expression can match the empty string, Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location.

如果 split 操作的执行时间超过为其调用该方法的应用程序域指定的超时间隔,则会引发 RegexMatchTimeoutException 异常。The RegexMatchTimeoutException exception is thrown if the execution time of the split operation exceeds the time-out interval specified for the application domain in which the method is called. 如果在应用程序域的属性中未定义超时值,或者如果 Regex.InfiniteMatchTimeout超时值,则不会引发异常。If no time-out is defined in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown.

调用方说明

此方法在与调用方法的应用程序域的默认超时值相等的时间间隔后超时。This method times out after an interval that is equal to the default time-out value of the application domain in which the method is called. 如果尚未为应用程序域定义超时值,则将使用值 InfiniteMatchTimeout,这会阻止方法超时。If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. 用于在模式匹配时拆分文本的建议静态方法是 Split(String, String, RegexOptions, TimeSpan),这允许您设置超时间隔。The recommended static method for splitting text on a pattern match is Split(String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

另请参阅

Split(String, Int32, Int32)

在由 Regex 构造函数中指定的正则表达式定义的位置,将输入字符串拆分为子字符串数组指定的最大次数。Splits an input string a specified maximum number of times into an array of substrings, at the positions defined by a regular expression specified in the Regex constructor. 从输入字符串的指定字符位置开始搜索正则表达式模式。The search for the regular expression pattern starts at a specified character position in the input string.

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

要拆分的字符串。The string to be split.

count
Int32

可拆分的最大次数。The maximum number of times the split can occur.

startat
Int32

输入字符串中将开始搜索的字符位置。The character position in the input string where the search will begin.

返回

String[]

字符串数组。An array of strings.

异常

inputnullinput is null.

startat 小于零或大于 input 的长度。startat is less than zero or greater than the length of input.

发生超时。A time-out occurred. 有关超时的详细信息,请参阅“备注”部分。For more information about time-outs, see the Remarks section.

注解

Regex.Split 方法与 String.Split 方法类似,不同之处在于 Regex.Split 在由正则表达式(而不是一组字符)确定的分隔符处拆分字符串。The Regex.Split methods are similar to the String.Split method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. count 参数指定向其中拆分 input 字符串的子字符串的最大数量;最后一个字符串包含字符串的撤消余数。The count parameter specifies the maximum number of substrings into which the input string is split; the last string contains the unsplit remainder of the string. 如果 count 值为零,则提供尽可能多的拆分的默认行为。A count value of zero provides the default behavior of splitting as many times as possible. startat 参数定义开始搜索第一个分隔符的点(可用于跳过前导空格)。The startat parameter defines the point at which the search for the first delimiter begins (this can be used for skipping leading white space).

如果在字符串的 count+ 1 位置中找不到匹配项,则该方法将返回包含 input 字符串的单元素数组。If no matches are found from the count+1 position in the string, the method returns a one-element array that contains the input string. 如果找到一个或多个匹配项,则返回数组的第一个元素包含字符串的第一部分,该字符串的第一个部分中的第一个字符到匹配项前一个字符。If one or more matches are found, the first element of the returned array contains the first portion of the string from the first character up to one character before the match.

如果多个匹配项彼此相邻并且找到的匹配项的数目至少小于 count,则将空字符串插入到数组中。If multiple matches are adjacent to one another and the number of matches found is at least two less than count, an empty string is inserted into the array. 同样,如果在 startat(字符串中的第一个字符)找到匹配项,则返回数组的第一个元素为空字符串。Similarly, if a match is found at startat, which is the first character in the string, the first element of the returned array is an empty string. 也就是说,将在确定匹配子字符串的数目是否等于 count的情况下,计算相邻匹配项产生的空字符串。That is, empty strings that result from adjacent matches are counted in determining whether the number of matched substrings equals count. 在下面的示例中,正则表达式 \d+ 用于查找字符串中数字字符的第一个子字符串的起始位置,然后从该位置开始最多拆分字符串三次。In the following example, the regular expression \d+ is used to find the starting position of the first substring of numeric characters in a string, and then to split the string a maximum of three times starting at that position. 由于正则表达式模式与输入字符串的开头匹配,因此返回的字符串数组包含空字符串、5个字符的字母字符串和字符串的其余部分。Because the regular expression pattern matches the beginning of the input string, the returned string array consists of an empty string, a five-character alphabetic string, and the remainder of the string,

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'

如果正则表达式中使用了捕获括号,则任何捕获的文本都将包含在拆分字符串数组中。If capturing parentheses are used in a regular expression, any captured text is included in the array of split strings. 但是,任何包含捕获文本的数组元素都不计入,以确定是否已达到 count的匹配项的数目。However, any array elements that contain captured text are not counted in determining whether the number of matches has reached count. 例如,将字符串 "" apple-apricot-plum-石榴-pineapple-粉 "拆分为最多四个子字符串,从字符串中的字符15开始,如以下代码所示。For example, splitting the string '"apple-apricot-plum-pear-pomegranate-pineapple-peach" into a maximum of four substrings beginning at character 15 in the string results in a seven-element array, as the following code shows.

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 的版本。However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. 在 .NET Framework 1.0 和1.1 中,如果在第一组捕获括号中找不到匹配项,则不会在返回的数组中包含从其他捕获括号中捕获的文本。In .NET Framework 1.0 and 1.1, if a match is not found within the first set of capturing parentheses, captured text from additional capturing parentheses is not included in the returned array. 从 .NET Framework 2.0 开始,所有捕获的文本也会添加到返回的数组中。Starting with the .NET Framework 2.0, all captured text is also added to the returned array. 例如,下面的代码使用两组捕获括号来提取字符串中的各个单词。For example, the following code uses two sets of capturing parentheses to extract the individual words in a string. 第一组捕获括号捕获连字符,第二组捕获竖线。The first set of capturing parentheses captures the hyphen, and the second set captures the vertical bar. 如果在 .NET Framework 1.0 或1.1 下编译并运行示例代码,它将排除竖线字符;如果编译并在 .NET Framework 2.0 或更高版本下运行,则它将包含它们。If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the vertical bar characters; if it is compiled and run under the .NET Framework 2.0 or later versions, it includes them.

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 会将字符串拆分为单字符字符串数组,因为在每个位置都可以找到空字符串分隔符。If the regular expression can match the empty string, Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location. 下面的示例将字符串 "字符" 拆分为输入字符串中包含的多个元素,以字符 "a" 开头。The following example splits the string "characters" into as many elements as the input string contains, starting with the character "a". 由于 null 字符串与输入字符串的末尾匹配,因此将在返回的数组的末尾插入一个空字符串。Because the null string matches the end of the input string, a null string is inserted at the end of the returned array.

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}

如果 split 操作的执行时间超过了 Regex.Regex(String, RegexOptions, TimeSpan) 构造函数指定的超时间隔,则会引发 RegexMatchTimeoutException 异常。The RegexMatchTimeoutException exception is thrown if the execution time of the split operation exceeds the time-out interval specified by the Regex.Regex(String, RegexOptions, TimeSpan) constructor. 如果在调用构造函数时未设置超时间隔,则当操作超过为创建 Regex 对象的应用程序域建立的任何超时值时,将引发异常。If you do not set a time-out interval when you call the constructor, the exception is thrown if the operation exceeds any time-out value established for the application domain in which the Regex object is created. 如果在 Regex 构造函数调用或应用程序域的属性中没有定义超时值,或者如果 Regex.InfiniteMatchTimeout超时值,则不会引发异常If no time-out is defined in the Regex constructor call or in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown

另请参阅

Split(String, Int32)

在由 Regex 构造函数中指定的正则表达式定义的位置,将输入字符串拆分为子字符串数组指定的最大次数。Splits an input string a specified maximum number of times into an array of substrings, at the positions defined by a regular expression specified in the Regex constructor.

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

要拆分的字符串。The string to be split.

count
Int32

可拆分的最大次数。The maximum number of times the split can occur.

返回

String[]

字符串数组。An array of strings.

异常

inputnullinput is null.

发生超时。A time-out occurred. 有关超时的详细信息,请参阅“备注”部分。For more information about time-outs, see the Remarks section.

注解

Regex.Split 方法与 String.Split 方法类似,不同之处在于 Regex.Split 在由正则表达式(而不是一组字符)确定的分隔符处拆分字符串。The Regex.Split methods are similar to the String.Split method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. count 参数指定可将 input 字符串拆分到的子字符串的最大数量;最后一个字符串包含字符串的撤消余数。The count parameter specifies the maximum number of substrings into which the input string can be split; the last string contains the unsplit remainder of the string. 如果 count 值为零,则提供尽可能多的拆分的默认行为。A count value of zero provides the default behavior of splitting as many times as possible.

如果多个匹配项彼此相邻,或者如果在 input的开头或末尾找到了匹配项,并且找到的匹配项的数目至少小于 count,则将空字符串插入到数组中。If multiple matches are adjacent to one another or if a match is found at the beginning or end of input, and the number of matches found is at least two less than count, an empty string is inserted into the array. 也就是说,在确定匹配子字符串的数目是否等于 count的情况下,将在输入字符串的开头或结尾产生的空字符串数进行计算。That is, empty strings that result from adjacent matches or from matches at the beginning or end of the input string are counted in determining whether the number of matched substrings equals count. 在下面的示例中,正则表达式 /d+ 用于将包含一个或多个十进制数字的输入字符串拆分为最多三个子字符串。In the following example, the regular expression /d+ is used to split an input string that includes one or more decimal digits into a maximum of three substrings. 由于输入字符串的开头与正则表达式模式匹配,因此第一个数组元素包含 String.Empty,第二个数组元素包含输入字符串中的第一组字母字符,第三个数组包含第三个匹配项后的字符串的剩余部分。Because the beginning of the input string matches the regular expression pattern, the first array element contains String.Empty, the second contains the first set of alphabetic characters in the input string, and the third contains the remainder of the string that follows the third match.

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'

如果正则表达式中使用了捕获括号,则任何捕获的文本都将包含在拆分字符串数组中。If capturing parentheses are used in a regular expression, any captured text is included in the array of split strings. 但是,任何包含捕获文本的数组元素都不计入,以确定是否已达到 count的匹配项的数目。However, any array elements that contain captured text are not counted in determining whether the number of matches has reached count. 例如,将字符串 "apricot-plum-香蕉" 拆分为最多四个子字符串会生成一个7元素数组,如以下代码所示。For example, splitting the string "apple-apricot-plum-pear-banana" into a maximum of four substrings results in a seven-element array, as the following code shows.

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 的版本。However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. 在 .NET Framework 1.0 和1.1 中,只在返回的数组中包含从第一组捕获括号中捕获的文本。In the .NET Framework 1.0 and 1.1, only captured text from the first set of capturing parentheses is included in the returned array. 从 .NET Framework 2.0 开始,所有捕获的文本都将添加到返回的数组中。Starting with the .NET Framework 2.0, all captured text is added to the returned array. 但是,在确定匹配子字符串的数目是否等于 count时,不会对返回的数组中包含捕获文本的元素进行计数。However, elements in the returned array that contain captured text are not counted in determining whether the number of matched substrings equals count. 例如,在下面的代码中,正则表达式使用两组捕获括号从日期字符串提取日期的元素。For example, in the following code, a regular expression uses two sets of capturing parentheses to extract the elements of a date from a date string. 第一组捕获括号捕获连字符,第二组捕获的是正斜杠。The first set of capturing parentheses captures the hyphen, and the second set captures the forward slash. 调用 Split(String, Int32) 方法,然后在返回的数组中指定最多两个元素。The call to the Split(String, Int32) method then specifies a maximum of two elements in the returned array. 如果在 .NET Framework 1.0 或1.1 下编译并运行示例代码,则该方法将返回一个双元素字符串数组。If the example code is compiled and run under the .NET Framework 1.0 or 1.1, the method returns a two-element string array. 如果编译并在 .NET Framework 2.0 或更高版本下运行,则该方法将返回一个由三个元素组成的字符串数组。If it is compiled and run under the .NET Framework 2.0 or later versions, the method returns a three-element string array.

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) 会将字符串拆分为单字符字符串数组,因为在每个位置都可以找到空字符串分隔符。If the regular expression can match the empty string, Split(String, Int32) will split the string into an array of single-character strings because the empty string delimiter can be found at every location. 下面的示例将字符串 "字符" 拆分为与输入字符串中的元素数量相同的元素。The following example splits the string "characters" into as many elements as there are in the input string. 由于 null 字符串与输入字符串的开头匹配,因此将在返回的数组的开头插入一个空字符串。Because the null string matches the beginning of the input string, a null string is inserted at the beginning of the returned array. 这将导致第十个元素由输入字符串末尾的两个字符组成。This causes the tenth element to consist of the two characters at the end of the input string.

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}

如果 split 操作的执行时间超过了 Regex.Regex(String, RegexOptions, TimeSpan) 构造函数指定的超时间隔,则会引发 RegexMatchTimeoutException 异常。The RegexMatchTimeoutException exception is thrown if the execution time of the split operation exceeds the time-out interval specified by the Regex.Regex(String, RegexOptions, TimeSpan) constructor. 如果在调用构造函数时未设置超时间隔,则当操作超过为创建 Regex 对象的应用程序域建立的任何超时值时,将引发异常。If you do not set a time-out interval when you call the constructor, the exception is thrown if the operation exceeds any time-out value established for the application domain in which the Regex object is created. 如果在 Regex 构造函数调用或应用程序域的属性中没有定义超时值,或者如果 Regex.InfiniteMatchTimeout超时值,则不会引发异常If no time-out is defined in the Regex constructor call or in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown

另请参阅

Split(String)

在由 Regex 构造函数指定的正则表达式模式所定义的位置,将输入字符串拆分为子字符串数组。Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.

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

要拆分的字符串。The string to split.

返回

String[]

字符串数组。An array of strings.

异常

inputnullinput is null.

发生超时。A time-out occurred. 有关超时的详细信息,请参阅“备注”部分。For more information about time-outs, see the Remarks section.

注解

Regex.Split 方法与 String.Split(Char[]) 方法类似,不同之处在于 Regex.Split 在由正则表达式(而不是一组字符)确定的分隔符处拆分字符串。The Regex.Split methods are similar to the String.Split(Char[]) method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. 尽可能多地拆分字符串。The string is split as many times as possible. 如果未找到分隔符,则返回值将包含一个值为原始输入字符串的元素。If no delimiter is found, the return value contains one element whose value is the original input string.

如果多个匹配项彼此相邻,则将空字符串插入到数组中。If multiple matches are adjacent to one another, an empty string is inserted into the array. 例如,将字符串拆分为单个连字符会导致返回的数组在找到两个相邻连字符的位置包含空字符串,如以下代码所示。For example, splitting a string on a single hyphen causes the returned array to include an empty string in the position where two adjacent hyphens are found, as the following code shows.

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'      

如果在输入字符串的开头或末尾找到了匹配项,则返回数组的开头或结尾处将包含空字符串。If a match is found at the beginning or the end of the input string, an empty string is included at the beginning or the end of the returned array. 下面的示例使用正则表达式模式 \d+ 来对数字字符拆分输入字符串。The following example uses the regular expression pattern \d+ to split an input string on numeric characters. 因为字符串以匹配的数字字符开始和结束,所以,返回数组的第一个元素和最后一个元素的值是 String.EmptyBecause the string begins and ends with matching numeric characters, the value of the first and last element of the returned array is 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 表达式中使用捕获括号,则生成的字符串数组中将包括任何捕获的文本。If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 例如,如果在放在捕获括号内的连字符处拆分字符串 "plum",则返回的数组包含一个包含连字符的字符串元素。For example, if you split the string "plum-pear" on a hyphen placed within capturing parentheses, the returned array includes a string element that contains the hyphen.

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 的版本。However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. 在 .NET Framework 1.0 和1.1 中,如果在第一组捕获括号中找不到匹配项,则不会在返回的数组中包含从其他捕获括号中捕获的文本。In the .NET Framework 1.0 and 1.1, if a match is not found within the first set of capturing parentheses, captured text from additional capturing parentheses is not included in the returned array. 从 .NET Framework 2.0 开始,所有捕获的文本也会添加到返回的数组中。Starting with the .NET Framework 2.0, all captured text is also added to the returned array. 例如,下面的代码使用两组捕获括号从日期字符串提取日期的元素,包括日期分隔符。For example, the following code uses two sets of capturing parentheses to extract the elements of a date, including the date delimiters, from a date string. 第一组捕获括号捕获连字符,第二组捕获的是正斜杠。The first set of capturing parentheses captures the hyphen, and the second set captures the forward slash. 如果在 .NET Framework 1.0 或1.1 下编译并运行示例代码,它将排除斜杠字符;如果编译并在 .NET Framework 2.0 或更高版本下运行,则它将包含它们。If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the slash characters; if it is compiled and run under the .NET Framework 2.0 or later versions, it includes them.

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) 会将字符串拆分为单字符字符串数组,因为在每个位置都可以找到空字符串分隔符。If the regular expression can match the empty string, Split(String) will split the string into an array of single-character strings because the empty string delimiter can be found at every location. 例如:For example:

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

请注意,返回的数组还在数组的开头和结尾包含空字符串。Note that the returned array also includes an empty string at the beginning and end of the array.

如果 split 操作的执行时间超过了 Regex.Regex(String, RegexOptions, TimeSpan) 构造函数指定的超时间隔,则会引发 RegexMatchTimeoutException 异常。The RegexMatchTimeoutException exception is thrown if the execution time of the split operation exceeds the time-out interval specified by the Regex.Regex(String, RegexOptions, TimeSpan) constructor. 如果在调用构造函数时未设置超时间隔,则当操作超过为创建 Regex 对象的应用程序域建立的任何超时值时,将引发异常。If you do not set a time-out interval when you call the constructor, the exception is thrown if the operation exceeds any time-out value established for the application domain in which the Regex object is created. 如果在 Regex 构造函数调用或应用程序域的属性中没有定义超时值,或者如果 Regex.InfiniteMatchTimeout超时值,则不会引发异常If no time-out is defined in the Regex constructor call or in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown

另请参阅

Split(String, String)

在由正则表达式模式定义的位置将输入字符串拆分为一个子字符串数组。Splits an input string into an array of substrings at the positions defined by a regular expression pattern.

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

要拆分的字符串。The string to split.

pattern
String

要匹配的正则表达式模式。The regular expression pattern to match.

返回

String[]

字符串数组。An array of strings.

异常

出现正则表达式分析错误。A regular expression parsing error occurred.

inputpatternnullinput or pattern is null.

发生超时。A time-out occurred. 有关超时的详细信息,请参阅“备注”部分。For more information about time-outs, see the Remarks section.

注解

Regex.Split 方法与 String.Split 方法类似,不同之处在于 Regex.Split 在由正则表达式(而不是一组字符)确定的分隔符处拆分字符串。The Regex.Split methods are similar to the String.Split method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. 尽可能多地拆分 input 字符串。The input string is split as many times as possible. 如果在 input 字符串中找不到 pattern,则返回值将包含一个值为原始 input 字符串的元素。If pattern is not found in the input string, the return value contains one element whose value is the original input string.

pattern 参数包含符号描述要匹配的字符串的正则表达式语言元素。The pattern parameter consists of regular expression language elements that symbolically describe the string to match. 有关正则表达式的详细信息,请参阅.NET Framework 正则表达式正则表达式语言-快速参考For more information about regular expressions, see .NET Framework Regular Expressions and Regular Expression Language - Quick Reference.

重要

对静态 Split 方法的调用中使用的已编译正则表达式会自动缓存。Compiled regular expressions used in calls to static Split methods are automatically cached. 若要自行管理已编译的正则表达式的生存期,请使用实例 Split 方法。To manage the lifetime of compiled regular expressions yourself, use the instance Split methods.

如果多个匹配项彼此相邻,则将空字符串插入到数组中。If multiple matches are adjacent to one another, an empty string is inserted into the array. 例如,将字符串拆分为单个连字符会导致返回的数组在找到两个相邻连字符的位置包含空字符串,如以下代码所示。For example, splitting a string on a single hyphen causes the returned array to include an empty string in the position where two adjacent hyphens are found, as the following code shows.

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'      

如果在输入字符串的开头或末尾找到了匹配项,则返回数组的开头或结尾处将包含空字符串。If a match is found at the beginning or the end of the input string, an empty string is included at the beginning or the end of the returned array. 下面的示例使用正则表达式模式 \d+ 来对数字字符拆分输入字符串。The following example uses the regular expression pattern \d+ to split an input string on numeric characters. 因为字符串以匹配的数字字符开始和结束,所以,返回数组的第一个元素和最后一个元素的值是 String.EmptyBecause the string begins and ends with matching numeric characters, the value of the first and last element of the returned array is 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 表达式中使用捕获括号,则生成的字符串数组中将包括任何捕获的文本。If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 例如,如果在放在捕获括号内的连字符处拆分字符串 "plum",则返回的数组包含一个包含连字符的字符串元素。For example, if you split the string "plum-pear" on a hyphen placed within capturing parentheses, the returned array includes a string element that contains the hyphen.

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 的版本。However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. 在 .NET Framework 1.0 和1.1 中,如果在第一组捕获括号中找不到匹配项,则不会在返回的数组中包含从其他捕获括号中捕获的文本。In the .NET Framework 1.0 and 1.1, if a match is not found within the first set of capturing parentheses, captured text from additional capturing parentheses is not included in the returned array. 从 .NET Framework 2.0 开始,所有捕获的文本也会添加到返回的数组中。Starting with the .NET Framework 2.0, all captured text is also added to the returned array. 例如,下面的代码使用两组捕获括号从日期字符串提取日期的元素,包括日期分隔符。For example, the following code uses two sets of capturing parentheses to extract the elements of a date, including the date delimiters, from a date string. 第一组捕获括号捕获连字符,第二组捕获的是正斜杠。The first set of capturing parentheses captures the hyphen, and the second set captures the forward slash. 如果在 .NET Framework 1.0 或1.1 下编译并运行示例代码,它将排除斜杠字符;如果编译并在 .NET Framework 2.0 或更高版本下运行,则它将包含它们。If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the slash characters; if it is compiled and run under the .NET Framework 2.0 or later versions, it includes them.

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 会将字符串拆分为单字符字符串数组,因为在每个位置都可以找到空字符串分隔符。If the regular expression can match the empty string, Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location. 例如:For example:

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

请注意,返回的数组还在数组的开头和结尾包含空字符串。Note that the returned array also includes an empty string at the beginning and end of the array.

如果 split 操作的执行时间超过为其调用该方法的应用程序域指定的超时间隔,则会引发 RegexMatchTimeoutException 异常。The RegexMatchTimeoutException exception is thrown if the execution time of the split operation exceeds the time-out interval specified for the application domain in which the method is called. 如果在应用程序域的属性中未定义超时值,或者如果 Regex.InfiniteMatchTimeout超时值,则不会引发异常。If no time-out is defined in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown.

调用方说明

此方法在与调用方法的应用程序域的默认超时值相等的时间间隔后超时。This method times out after an interval that is equal to the default time-out value of the application domain in which the method is called. 如果尚未为应用程序域定义超时值,则将使用值 InfiniteMatchTimeout,这会阻止方法超时。If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. 用于在模式匹配时拆分文本的建议静态方法是 Split(String, String, RegexOptions, TimeSpan),这允许您设置超时间隔。The recommended static method for splitting text on a pattern match is Split(String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

另请参阅

适用于