Regex.Split Method (String, String)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Splits the input string at the positions defined by a regular expression pattern.
Assembly: System (in System.dll)
'Declaration Public Shared Function Split ( _ input As String, _ pattern As String _ ) As String()
Parameters
- input
- Type: System.String
The string to split.
- pattern
- Type: System.String
The regular expression pattern to match.
| Exception | Condition |
|---|---|
| ArgumentException | A regular expression parsing error occurred. |
| ArgumentNullException | input is Nothing. -or- pattern is Nothing. |
The Regex.Split methods are similar to the String.Split method, except 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 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 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 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 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 outputBlock.Text += String.Format("'{0}'", match) & vbCrLf Next ' 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 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 outputBlock.Text += String.Format("'{0}'", match) & vbCrLf Next ' 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 returned array includes the slash characters.
Dim input As String = "07/14/2007" Dim pattern As String = "(-)|(/)" For Each result As String In Regex.Split(input, pattern) outputBlock.Text += String.Format("'{0}'", result) & vbCrLf 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'
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:
Dim input As String = "characters" Dim substrings() As String = Regex.Split(input, "") outputBlock.Text &= "{" For ctr As Integer = 0 To substrings.Length - 1 outputBlock.Text += String.Format("'{0}'", 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', ''}
Note that the returned array also includes an empty string at the beginning and end of the array.
Important Note: