Split Method (String, String, RegexOptions, TimeSpan)

Regex.Split Method (String, String, RegexOptions, TimeSpan)

.NET Framework (current version)
 

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.

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

Public Shared Function Split (
	input As String,
	pattern As String,
	options As RegexOptions,
	matchTimeout As TimeSpan
) As 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.

matchTimeout
Type: System.TimeSpan

A time-out interval, or Regex.InfiniteMatchTimeout to indicate that the method should not time out.

Return Value

Type: System.String()

A string array.

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.

-or-

matchTimeout is negative, zero, or greater than approximately 24 days.

RegexMatchTimeoutException

A time-out occurred. For more information about time-outs, see the Remarks section.

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 Framework Regular Expressions and Regular Expression Language - Quick Reference.

System_CAPS_importantImportant

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.

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.

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.

Imports System
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 in the .NET Framework and Backtracking in Regular Expressions. 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 Regex.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.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft