Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Regex.Split Method (String, String, RegexOptions)

Splits the input string at the positions defined by a specified regular expression pattern. Specified options modify the matching operation.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

public static string[] Split(
	string input,
	string pattern,
	RegexOptions options
)

Parameters

input
Type: System.String
The string to split.
pattern
Type: System.String
The regular expression pattern to match.
options
Type: System.Text.RegularExpressions.RegexOptions
A bitwise combination of the enumeration values.

Return Value

Type: System.String[]
An array of strings.

ExceptionCondition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input is null.

-or-

pattern is null.

ArgumentOutOfRangeException

options is not a valid bitwise combination of RegexOptions values.

The Regex.Split methods are similar to the String.Split(Char[]) method, except this method 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 parameter string.

The pattern parameter consists of various regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see Regular Expression Language Elements in the .NET Framework documentation.

Important noteImportant Note:

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 capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. For example, splitting the string " plum-pear" on a hyphen placed within capturing parentheses adds a string element that contains the hyphen to the returned array.


string input = "plum-pear";
string pattern = "(-)";

string[] substrings = Regex.Split(input, pattern);    // Split on hyphens
foreach (string match in substrings)
{
   outputBlock.Text += String.Format("'{0}'", match) + "\n";
}
// The method writes the following:
//    'plum'
//    '-'
//    'pear'      


When the regular expression pattern includes multiple sets of capturing parentheses, 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, while the second set captures the forward slash. The returned array includes the slash characters.


string input = @"07/14/2007";
string pattern = @"(-)|(/)";

foreach (string result in Regex.Split(input, pattern))
{
   outputBlock.Text += String.Format("'{0}'", result) + "\n";
}
// In .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// In .NET 2.0, 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.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2015 Microsoft