Regex.Split Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Splits an input string into an array of substrings at the positions defined by a regular expression match.
Overloads
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) |
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, String) |
Splits an input string into an array of substrings at the positions defined by a regular expression pattern. |
Split(String) |
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, Int32) |
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, String, RegexOptions, TimeSpan)
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
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()
Parameters
- 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
A time-out interval, or InfiniteMatchTimeout to indicate that the method should not time out.
Returns
A string array.
Exceptions
A regular expression parsing error occurred.
input
or pattern
is null
.
options
is not a valid bitwise combination of RegexOptions values.
-or-
matchTimeout
is negative, zero, or greater than approximately 24 days.
A time-out occurred. For more information about time-outs, see the Remarks section.
Remarks
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.
The pattern
parameter consists of regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see .NET Regular Expressions and Regular Expression Language - Quick Reference.
Important
Compiled regular expressions used in calls to static Split methods are automatically cached. 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. The following example uses the regular expression pattern [a-z]+
to split an input string on any uppercase or lowercase alphabetic character. Because 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', ''
If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 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'
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. 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. 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. 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'
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.
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. If no match is found in that time interval, the method throws a RegexMatchTimeoutException exception. matchTimeout
overrides any default time-out value defined for the application domain in which the method executes.
Notes to Callers
We recommend that you set the matchTimeout
parameter to an appropriate value, such as two seconds. 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.
See also
Applies to
Split(String, String, RegexOptions)
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
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()
Parameters
- 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.
Returns
An array of strings.
Exceptions
A regular expression parsing error occurred.
input
or pattern
is null
.
options
is not a valid bitwise combination of RegexOptions values.
A time-out occurred. For more information about time-outs, see the Remarks section.
Remarks
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.
The pattern
parameter consists of regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see .NET Regular Expressions and Regular Expression Language - Quick Reference.
Important
Compiled regular expressions used in calls to static Split methods are automatically cached. 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. The following example uses the regular expression pattern [a-z]+
to split an input string on any uppercase or lowercase alphabetic character. Because 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', ''
If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 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'
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. 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. 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. 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'
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.
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. 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.
Notes to Callers
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. 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. 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.
See also
Applies to
Split(String, Int32, Int32)
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
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()
Parameters
- 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.
Returns
An array of strings.
Exceptions
input
is null
.
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.
Remarks
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. 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. A count
value of zero provides the default behavior of splitting as many times as possible. The startat
parameter defines the point at which the search for the first delimiter begins (this can be used for skipping leading white space).
For more details about startat
, see the Remarks section of Match(String, Int32).
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.
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. 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. That is, empty strings that result from adjacent matches are counted in determining whether the number of matched substrings equals count
. 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. 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. However, any array elements that contain captured text are not counted in determining whether the number of matches has reached count
. 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.
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'
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. 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. 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. 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'
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. The following example splits the string "characters" into as many elements as the input string contains, starting with the character "a". 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}
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. 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. 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
See also
Applies to
Split(String, String)
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
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()
Parameters
- input
- String
The string to split.
- pattern
- String
The regular expression pattern to match.
Returns
An array of strings.
Exceptions
A regular expression parsing error occurred.
input
or pattern
is null
.
A time-out occurred. For more information about time-outs, see the Remarks section.
Remarks
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. The input
string is split as many times as possible. If pattern
is not found in the input
string, the return value contains one element whose value is the original input
string.
The pattern
parameter consists of regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see .NET Regular Expressions and Regular Expression Language - Quick Reference.
Important
Compiled regular expressions used in calls to static Split methods are automatically cached. 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. The following example uses the regular expression pattern \d+
to split an input string on numeric characters. Because 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', ''
If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 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'
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. 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. 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. 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'
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.
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. 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.
Notes to Callers
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. 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. 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.
See also
Applies to
Split(String)
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
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()
Parameters
- input
- String
The string to split.
Returns
An array of strings.
Exceptions
input
is null
.
A time-out occurred. For more information about time-outs, see the Remarks section.
Remarks
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. The following example uses the regular expression pattern \d+
to split an input string on numeric characters. Because 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', ''
If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. 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'
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. 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. 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. 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'
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.
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. 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. 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
See also
Applies to
Split(String, Int32)
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
- Source:
- Regex.Split.cs
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()
Parameters
- input
- String
The string to be split.
- count
- Int32
The maximum number of times the split can occur.
Returns
An array of strings.
Exceptions
input
is null
.
A time-out occurred. For more information about time-outs, see the Remarks section.
Remarks
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. 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. A count
value of zero provides the default behavior of splitting as many times as possible.
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. 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
. 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. 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. However, any array elements that contain captured text are not counted in determining whether the number of matches has reached count
. 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'
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. 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. Starting with the .NET Framework 2.0, all captured text is added to the returned array. 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. The call to the Split(String, Int32) method then specifies a maximum of two elements in the returned array. 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. 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'
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. 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}
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. 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. 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