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)
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
,则不会引发异常。
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()
这些
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(", ")
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)
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)
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
来禁用超时,则正则表达式引擎提供的性能稍好一些。 但是,应仅在以下情况下禁用超时:
当正则表达式处理的输入派生自已知且受信任的源或由静态文本组成时。 这不包括用户动态输入的文本。
当正则表达式模式经过全面测试以确保它有效地处理匹配项、非匹配项和接近匹配项时。
当正则表达式模式不包含已知在处理接近匹配时导致过度回溯的语言元素时。