Regex.Split Method (String, String, RegexOptions) (System.Text.RegularExpressions)

Switch View :
ScriptFree
.NET Framework Class Library
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)
Syntax

Visual Basic
Public Shared Function Split ( _
	input As String, _
	pattern As String, _
	options As RegexOptions _
) As String()
C#
public static string[] Split(
	string input,
	string pattern,
	RegexOptions options
)
Visual C++
public:
static array<String^>^ Split(
	String^ input, 
	String^ pattern, 
	RegexOptions options
)
F#
static member Split : 
        input:string * 
        pattern:string * 
        options:RegexOptions -> string[] 

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 that provide options for matching.

Return Value

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

Exception Condition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input or pattern is null.

ArgumentOutOfRangeException

options is not a valid bitwise combination of RegexOptions values.

Remarks

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 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 .NET Framework Regular Expressions and Regular Expression Language Elements.

Important note 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 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.

Visual Basic

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
' The method writes the following to the console:
'    'plum'
'    '-'
'    'pear'      


C#

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 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 versions 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. In the .NET Framework version 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, while the second set captures the forward slash. If the sample 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, it includes them.

Visual Basic

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


C#

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

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Other Resources