Compartir a través de


Regex.Split Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Splits the specified input string at the positions defined by a regular expression pattern specified in the Regex constructor.

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

Syntax

'Declaration
Public Function Split ( _
    input As String _
) As String()
public string[] Split(
    string input
)

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

input is nulla null reference (Nothing in Visual Basic).

Remarks

The Regex.Split(String) method is similar to the String.Split(array<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.

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.

Dim regex As Regex = New Regex("-")         ' Split on hyphens.
Dim substrings() As String = regex.Split("plum--pear")
For Each match As String In substrings
   outputBlock.Text += String.Format("'{0}'", match) & vbCrLf
Next
' The method writes the following:
'    'plum'
'    ''
'    'pear'      
Regex regex = new Regex("-");         // Split on hyphens.
string[] substrings = regex.Split("plum--pear");
foreach (string match in substrings)
{
   outputBlock.Text += String.Format("'{0}'", match) + "\n";
}
// The method writes the following:
//    'plum'
//    ''
//    'pear'      

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.

Dim regex As Regex = New Regex("(-)")          ' Split on hyphens.
Dim substrings() As String = regex.Split("plum-pear")
For Each match As String In substrings
   outputBlock.Text += String.Format("'{0}'", match) & vbCrLf
Next
' The method writes the following:
'    'plum'
'    '-'
'    'pear'      
Regex regex = new Regex("(-)");         // Split on hyphens.
string[] substrings = regex.Split("plum-pear");
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 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 input string uses forward slashes as delimiters and they are included in the array returned by the Regex.Split(String) method.

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)
   outputBlock.Text += String.Format("'{0}'", result) & vbCrLf
Next
' The method returns an array of 5 elements, as follows:
'    '07'
'    '/'
'    '14'
'    '/'
'    '2007' 
string input = @"07/14/2007";
string pattern = @"(-)|(/)";
Regex regex = new Regex(pattern);
foreach (string result in regex.Split(input))
{
   outputBlock.Text += String.Format("'{0}'", result) + "\n";
}
// 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:

Dim input As String = "characters"
Dim regex As New Regex("")
Dim substrings() As String = regex.Split(input)
outputBlock.Text &= "{"
For ctr As Integer = 0 To substrings.Length - 1
   outputBlock.Text &= substrings(ctr)
   If ctr < substrings.Length - 1 Then outputBlock.Text &= ", "
Next
outputBlock.Text &= "}" & vbCrLf
' The example produces the following output:   
'    {, c, h, a, r, a, c, t, e, r, s, }
string input = "characters";
Regex regex = new Regex("");
string[] substrings = regex.Split(input);
outputBlock.Text += "{";
for (int ctr = 0; ctr < substrings.Length; ctr++)
{
   outputBlock.Text += substrings[ctr];
   if (ctr < substrings.Length - 1)
      outputBlock.Text += ", ";
}
outputBlock.Text += "}" + "\n";
// 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.

Version Information

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

Platforms

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