Regex.Split 메서드

정의

정규식 일치에 의해 정의된 위치에서 부분 문자열로 이루어진 배열로 입력 문자열을 분할합니다.

오버로드

Split(String)

Regex 생성자에서 지정된 정규식 패턴에 의해 정의된 위치에서 입력 문자열을 부분 문자열의 배열로 분할합니다.

Split(String, Int32)

입력 문자열을 Regex 생성자에서 지정된 정규식에 의해 정의된 위치에서 지정된 최대 수만큼 부분 문자열의 배열로 분할합니다.

Split(String, String)

정규식 패턴에 의해 정의된 위치에서 부분 문자열로 이루어진 배열로 입력 문자열을 분할합니다.

Split(String, Int32, Int32)

입력 문자열을 Regex 생성자에서 지정된 정규식에 의해 정의된 위치에서 지정된 최대 수만큼 부분 문자열의 배열로 분할합니다. 입력 문자열에서 지정된 문자 위치부터 정규식 패턴을 검색합니다.

Split(String, String, RegexOptions)

지정된 정규식 패턴에 의해 정의된 위치에서 부분 문자열로 이루어진 배열로 입력 문자열을 분할합니다. 지정한 옵션에 따라 일치 작업이 수정됩니다.

Split(String, String, RegexOptions, TimeSpan)

지정된 정규식 패턴에 의해 정의된 위치에서 부분 문자열로 이루어진 배열로 입력 문자열을 분할합니다. 추가 매개 변수는 일치하는 항목이 없는 경우 제한 시간 간격과 일치 작업을 수정하는 옵션을 지정합니다.

Split(String)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
Regex.Split.cs

Regex 생성자에서 지정된 정규식 패턴에 의해 정의된 위치에서 입력 문자열을 부분 문자열의 배열로 분할합니다.

public:
 cli::array <System::String ^> ^ Split(System::String ^ input);
public string[] Split (string input);
member this.Split : string -> string[]
Public Function Split (input As String) As String()

매개 변수

input
String

분할할 문자열입니다.

반환

String[]

문자열 배열입니다.

예외

input이(가) null인 경우

시간이 초과되었습니다. 시간 제한에 대한 자세한 내용은 설명 섹션을 참조하세요.

설명

메서드는 Regex.Split 문자열을 String.Split(Char[]) 문자 집합 대신 정규식에 의해 결정되는 구분 기호로 분할한다는 Regex.Split 점을 제외하고 메서드와 유사합니다. 문자열은 가능한 한 여러 번 분할됩니다. 구분 기호를 찾을 수 없는 경우 반환 값은 원래 입력 문자열인 하나의 요소를 포함합니다.

여러 일치 항목이 서로 인접한 경우 빈 문자열이 배열에 삽입됩니다. 예를 들어 단일 하이픈에서 문자열을 분할하면 다음 코드와 같이 두 개의 인접 하이픈이 있는 위치에 반환된 배열에 빈 문자열이 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      Regex regex = new Regex("-");         // Split on hyphens.
      string[] substrings = regex.Split("plum--pear");
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//    'plum'
//    ''
//    'pear'
Imports System.Text.RegularExpressions

Module RegexSplit
   Public Sub Main()
      Dim regex As Regex = New Regex("-")         ' Split on hyphens.
      Dim substrings() As String = regex.Split("plum--pear")
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The example displays the following output:
'    'plum'
'    ''
'    'pear'

입력 문자열의 시작 또는 끝에 일치하는 항목이 있으면 반환된 배열의 시작 또는 끝에 빈 문자열이 포함됩니다. 다음 예제에서는 정규식 패턴을 \d+ 사용하여 숫자 문자에 대한 입력 문자열을 분할합니다. 문자열이 시작되고 일치하는 숫자 문자로 끝나기 때문에 반환된 배열의 첫 번째 요소와 마지막 요소의 값은 입니다 String.Empty.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      
      string[] result = rgx.Split(input);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim rgx As New Regex(pattern)
      Dim input As String = "123ABCDE456FGHIJKL789MNOPQ012"
      
      Dim result() As String = rgx.Split(input)
      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:
'       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''

괄호 캡처가 식에 Regex.Split 사용되는 경우 캡처된 모든 텍스트가 결과 문자열 배열에 포함됩니다. 예를 들어 캡처하는 괄호 안에 배치된 하이픈에서 문자열 "plum-pear"를 분할하는 경우 반환된 배열에는 하이픈이 포함된 문자열 요소가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      Regex regex = new Regex("(-)");         // Split on hyphens.
      string[] substrings = regex.Split("plum-pear");
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//    'plum'
//    '-'
//    'pear'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim regex As Regex = New Regex("(-)")          ' Split on hyphens.
      Dim substrings() As String = regex.Split("plum-pear")
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The example displays the following output:
'    'plum'
'    '-'
'    'pear'

그러나 정규식 패턴에 여러 캡처 괄호 집합이 포함된 경우 이 메서드의 동작은 .NET Framework 버전에 따라 달라집니다. .NET Framework 1.0 및 1.1에서 첫 번째 캡처 괄호 집합 내에서 일치하는 항목을 찾을 수 없는 경우 추가 캡처 괄호에서 캡처한 텍스트는 반환된 배열에 포함되지 않습니다. .NET Framework 2.0부터 캡처된 모든 텍스트도 반환된 배열에 추가됩니다. 예를 들어 다음 코드는 두 세트의 캡처 괄호를 사용하여 날짜 문자열에서 날짜 구분 기호를 포함하여 날짜의 요소를 추출합니다. 첫 번째 캡처 괄호 집합은 하이픈을 캡처하고 두 번째 집합은 슬래시를 캡처합니다. 예제 코드가 컴파일되고 .NET Framework 1.0 또는 1.1에서 실행되는 경우 슬래시 문자를 제외합니다. 컴파일되어 .NET Framework 2.0 이상 버전에서 실행되는 경우 해당 문자가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";
      Regex regex = new Regex(pattern);
      foreach (string result in regex.Split(input)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// Under .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// Under .NET 2.0 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      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) 
         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'

정규식이 빈 문자열 Split(String) 과 일치할 수 있는 경우 빈 문자열 구분 기호는 모든 위치에서 찾을 수 있으므로 문자열을 단일 문자열 배열로 분할합니다. 예를 들면 다음과 같습니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      Regex regex = new Regex("");
      string[] substrings = regex.Split(input);
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write(substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
// The example displays the following output:   
//    {, c, h, a, r, a, c, t, e, r, s, }
Imports System.Text.RegularExpressions

Module Main
   Public Sub Main()
      Dim input As String = "characters"
      Dim regex As New Regex("")
      Dim substrings() As String = regex.Split(input)
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write(substrings(ctr))
         If ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example produces the following output:   
'    {, c, h, a, r, a, c, t, e, r, s, }

반환된 배열에는 배열의 시작과 끝에 빈 문자열도 포함됩니다.

RegexMatchTimeoutException 분할 작업의 실행 시간이 생성자가 지정한 제한 시간 간격을 초과하면 예외가 Regex.Regex(String, RegexOptions, TimeSpan) throw됩니다. 생성자를 호출할 때 시간 제한 간격을 설정 하지 않으면, 작업이 있는 애플리케이션 도메인에 설정 된 제한 시간 값을 초과 하면 예외가 throw 되는 Regex 개체가 만들어집니다. 제한 시간에 정의 된 경우는 Regex 생성자 호출 또는 애플리케이션 도메인의 속성 또는 시간 제한 값이 Regex.InfiniteMatchTimeout, 예외가 throw 되지 않습니다

추가 정보

적용 대상

Split(String, Int32)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
Regex.Split.cs

입력 문자열을 Regex 생성자에서 지정된 정규식에 의해 정의된 위치에서 지정된 최대 수만큼 부분 문자열의 배열로 분할합니다.

public:
 cli::array <System::String ^> ^ Split(System::String ^ input, int count);
public string[] Split (string input, int count);
member this.Split : string * int -> string[]
Public Function Split (input As String, count As Integer) As String()

매개 변수

input
String

분할될 문자열입니다.

count
Int32

분할할 수 있는 최대 횟수입니다.

반환

String[]

문자열 배열입니다.

예외

input이(가) null인 경우

시간이 초과되었습니다. 시간 제한에 대한 자세한 내용은 설명 섹션을 참조하세요.

설명

메서드는 Regex.Split 문자열을 String.Split 문자 집합 대신 정규식에 의해 결정되는 구분 기호로 분할한다는 Regex.Split 점을 제외하고 메서드와 유사합니다. 매개 변수는 count 문자열을 분할할 수 있는 input 최대 부분 문자열 수를 지정합니다. 마지막 문자열에는 문자열의 플리트되지 않은 나머지가 포함됩니다. 값이 0이 count 면 가능한 한 여러 번 분할하는 기본 동작이 제공됩니다.

여러 일치 항목이 서로 인접하거나 의 시작 또는 끝에서 input일치 항목이 발견되고 찾은 일치 항목 수가 보다 2개 미만 count인 경우 빈 문자열이 배열에 삽입됩니다. 즉, 입력 문자열의 시작 또는 끝에 있는 인접 일치 항목 또는 일치 항목에서 발생하는 빈 문자열은 일치하는 부분 문자열 수가 와 같은지 여부를 결정할 때 계산됩니다 count. 다음 예제에서는 정규식을 /d+ 사용하여 하나 이상의 10진수를 포함하는 입력 문자열을 최대 3개의 부분 문자열로 분할합니다. 입력 문자열의 시작이 정규식 패턴과 일치하기 때문에 첫 번째 배열 요소에는 가 포함 String.Empty되고, 두 번째 배열은 입력 문자열에 첫 번째 알파벳 문자 집합을 포함하고, 세 번째 배열은 세 번째 일치 항목 뒤에 있는 문자열의 나머지를 포함합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      
      string[] result = rgx.Split(input, 3);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL789MNOPQ012'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim rgx As New Regex(pattern)
      Dim input As String = "123ABCDE456FGHIJKL789MNOPQ012"
      
      Dim result() As String = rgx.Split(input, 3)
      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:
'       '', 'ABCDE', 'FGHIJKL789MNOPQ012'

캡처 괄호를 정규식에 사용하는 경우 캡처된 텍스트는 분할 문자열 배열에 포함됩니다. 그러나 캡처된 텍스트가 포함된 배열 요소는 일치 항목 수가 에 도달 count했는지 여부를 결정할 때 계산되지 않습니다. 예를 들어 다음 코드와 같이 문자열 "apple-apricot-plum-pear-banana"를 최대 4개의 부분 문자열로 분할하면 7개 요소 배열이 발생합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)";
      string input = "apple-apricot-plum-pear-banana";
      Regex regex = new Regex(pattern);         // Split on hyphens.
      string[] substrings = regex.Split(input, 4);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The example displays the following output:
//       'apple'
//       '-'
//       'apricot'
//       '-'
//       'plum'
//       '-'
//       'pear-banana'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(-)"
      Dim input As String = "apple-apricot-plum-pear-banana"
      Dim regex As Regex = New Regex(pattern)         ' Split on hyphens.
      Dim substrings() As String = regex.Split(input, 4)
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' The example displays the following output:
'    'apple'
'    '-'
'    'apricot'
'    '-'
'    'plum'
'    '-'
'    'pear-banana'

그러나 정규식 패턴에 여러 캡처 괄호 집합이 포함된 경우 이 메서드의 동작은 .NET Framework 버전에 따라 달라집니다. .NET Framework 1.0 및 1.1에서는 첫 번째 캡처 괄호 집합의 캡처된 텍스트만 반환된 배열에 포함됩니다. .NET Framework 2.0부터 캡처된 모든 텍스트가 반환된 배열에 추가됩니다. 그러나 캡처된 텍스트를 포함하는 반환된 배열의 요소는 일치하는 부분 문자열 수가 과 같은 count지 여부를 결정할 때 계산되지 않습니다. 예를 들어 다음 코드에서 정규식은 두 세트의 캡처 괄호를 사용하여 날짜 문자열에서 날짜의 요소를 추출합니다. 첫 번째 캡처 괄호 집합은 하이픈을 캡처하고 두 번째 집합은 슬래시를 캡처합니다. 메서드에 대한 Split(String, Int32) 호출은 반환된 배열에서 최대 두 개의 요소를 지정합니다. 예제 코드가 컴파일되고 .NET Framework 1.0 또는 1.1에서 실행되는 경우 메서드는 2개 요소 문자열 배열을 반환합니다. 컴파일되고 .NET Framework 2.0 이상 버전에서 실행되는 경우 메서드는 3개 요소 문자열 배열을 반환합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";
      Regex regex = new Regex(pattern);
      foreach (string result in regex.Split(input, 2)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// Under .NET 1.0 and 1.1, the method returns an array of
// 2 elements, as follows:
//    '07'
//    '14/2007'
//
// Under .NET 2.0 and later, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '/'
//    '14/2007'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      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, 2) 
         Console.WriteLine("'{0}'", result)
      Next
   End Sub
End Module
' In .NET 1.0 and 1.1, the method returns an array of
' 2 elements, as follows:
'    '07'
'    '14/2007'
'
' In .NET 2.0 and later, the method returns an array of
' 3 elements, as follows:
'    '07'
'    '/'
'    '14/2007'

정규식이 빈 문자열 Split(String, Int32) 과 일치할 수 있는 경우 빈 문자열 구분 기호는 모든 위치에서 찾을 수 있으므로 문자열을 단일 문자열 배열로 분할합니다. 다음 예제에서는 문자열 "characters"를 입력 문자열에 있는 만큼의 요소로 분할합니다. null 문자열은 입력 문자열의 시작과 일치하므로 반환된 배열의 시작 부분에 null 문자열이 삽입됩니다. 이렇게 하면 10번째 요소가 입력 문자열의 끝에 있는 두 문자로 구성됩니다.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "characters"
      Dim regex As New Regex("")
      Dim substrings() As String = regex.Split(input, input.Length)
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write(substrings(ctr))
         if ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example displays the following output:   
'    {, c, h, a, r, a, c, t, e, rs}

RegexMatchTimeoutException 분할 작업의 실행 시간이 생성자가 지정한 제한 시간 간격을 초과하면 예외가 Regex.Regex(String, RegexOptions, TimeSpan) throw됩니다. 생성자를 호출할 때 시간 제한 간격을 설정 하지 않으면, 작업이 있는 애플리케이션 도메인에 설정 된 제한 시간 값을 초과 하면 예외가 throw 되는 Regex 개체가 만들어집니다. 제한 시간에 정의 된 경우는 Regex 생성자 호출 또는 애플리케이션 도메인의 속성 또는 시간 제한 값이 Regex.InfiniteMatchTimeout, 예외가 throw 되지 않습니다

추가 정보

적용 대상

Split(String, String)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
Regex.Split.cs

정규식 패턴에 의해 정의된 위치에서 부분 문자열로 이루어진 배열로 입력 문자열을 분할합니다.

public:
 static cli::array <System::String ^> ^ Split(System::String ^ input, System::String ^ pattern);
public static string[] Split (string input, string pattern);
static member Split : string * string -> string[]
Public Shared Function Split (input As String, pattern As String) As String()

매개 변수

input
String

분할할 문자열입니다.

pattern
String

일치 항목을 찾을 정규식 패턴입니다.

반환

String[]

문자열 배열입니다.

예외

정규식 구문 분석 오류가 발생했습니다.

input 또는 patternnull인 경우

시간이 초과되었습니다. 시간 제한에 대한 자세한 내용은 설명 섹션을 참조하세요.

설명

메서드는 Regex.Split 문자열을 String.Split 문자 집합 대신 정규식에 의해 결정되는 구분 기호로 분할한다는 Regex.Split 점을 제외하고 메서드와 유사합니다. 문자열은 input 가능한 한 여러 번 분할됩니다. 가 문자열에 input 없는 경우 pattern 반환 값은 원래 input 문자열인 하나의 요소를 포함합니다.

매개 변수는 pattern 일치시킬 문자열을 상징적으로 설명하는 정규식 언어 요소로 구성됩니다. 정규식에 대한 자세한 내용은 .NET 정규식정규식 언어 - 빠른 참조를 참조하세요.

중요

정적 메서드 호출에 사용되는 컴파일된 정규 Split 식은 자동으로 캐시됩니다. 컴파일된 정규식의 수명을 직접 관리하려면 instance Split 메서드를 사용합니다.

여러 일치 항목이 서로 인접한 경우 빈 문자열이 배열에 삽입됩니다. 예를 들어 단일 하이픈에서 문자열을 분할하면 다음 코드와 같이 두 개의 인접 하이픈이 있는 위치에 반환된 배열에 빈 문자열이 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum--pear";
      string pattern = "-";            // Split on hyphens
      
      string[] substrings = Regex.Split(input, pattern);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The method displays the following output:
//    'plum'
//    ''
//    'pear'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      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
         Console.WriteLine("'{0}'", match)
      Next
   End Sub  
End Module
' The example displays the following output:
'    'plum'
'    ''
'    'pear'

입력 문자열의 시작 또는 끝에 일치하는 항목이 있으면 반환된 배열의 시작 또는 끝에 빈 문자열이 포함됩니다. 다음 예제에서는 정규식 패턴을 \d+ 사용하여 숫자 문자에 대한 입력 문자열을 분할합니다. 문자열이 시작되고 일치하는 숫자 문자로 끝나기 때문에 반환된 배열의 첫 번째 요소와 마지막 요소의 값은 입니다 String.Empty.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      string[] result = Regex.Split(input, pattern);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim input As String = "123ABCDE456FGHIJKL789MNOPQ012"
      Dim result() As String = Regex.Split(input, pattern)
      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:
'       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''

괄호 캡처가 식에 Regex.Split 사용되는 경우 캡처된 모든 텍스트가 결과 문자열 배열에 포함됩니다. 예를 들어 캡처하는 괄호 안에 배치된 하이픈에서 문자열 "plum-pear"를 분할하는 경우 반환된 배열에는 하이픈이 포함된 문자열 요소가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      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 example displays the following output:
//    'plum'
//    '-'
//    'pear'
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'

그러나 정규식 패턴에 여러 캡처 괄호 집합이 포함된 경우 이 메서드의 동작은 .NET Framework 버전에 따라 달라집니다. .NET Framework 1.0 및 1.1에서 첫 번째 캡처 괄호 집합 내에서 일치하는 항목을 찾을 수 없는 경우 추가 캡처 괄호에서 캡처한 텍스트는 반환된 배열에 포함되지 않습니다. .NET Framework 2.0부터 캡처된 모든 텍스트도 반환된 배열에 추가됩니다. 예를 들어 다음 코드는 두 세트의 캡처 괄호를 사용하여 날짜 문자열에서 날짜 구분 기호를 포함하여 날짜의 요소를 추출합니다. 첫 번째 캡처 괄호 집합은 하이픈을 캡처하고 두 번째 집합은 슬래시를 캡처합니다. 예제 코드가 컴파일되고 .NET Framework 1.0 또는 1.1에서 실행되는 경우 슬래시 문자를 제외합니다. 컴파일되어 .NET Framework 2.0 이상 버전에서 실행되는 경우 해당 문자가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      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 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
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'

정규식이 빈 문자열 Split 과 일치할 수 있는 경우 빈 문자열 구분 기호는 모든 위치에서 찾을 수 있으므로 문자열을 단일 문자열 배열로 분할합니다. 예를 들면 다음과 같습니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      string[] substrings = Regex.Split(input, "");
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write("'{0}'", substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
// The example produces the following output:   
//    {'', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ''}
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "characters"
      Dim substrings() As String = Regex.Split(input, "")
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write("'{0}'", substrings(ctr))
         If ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example produces the following output:   
'    {'', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ''}

반환된 배열에는 배열의 시작과 끝에 빈 문자열도 포함됩니다.

RegexMatchTimeoutException split 작업의 실행 시간이 메서드가 호출 되는 애플리케이션 도메인에 대 한 지정 된 시간 제한 간격을 초과 하는 경우 예외가 throw 됩니다. 시간 제한이 없으며 애플리케이션 도메인의 속성에 정의 된 제한 시간 값이 아니면 Regex.InfiniteMatchTimeout, 예외가 throw 되지 않습니다.

호출자 참고

이 메서드는 메서드가 호출 되는 애플리케이션 도메인의 기본 제한 시간 값에 해당 간격이 지난 후 시간이 합니다. 애플리케이션 도메인에 값에 대 한 제한 시간 값을 정의 되지 않은 경우 InfiniteMatchTimeout, 메서드 시간 초과 방지 하는는 데 사용 됩니다. 패턴 일치에서 텍스트를 분할하는 데 권장되는 정적 메서드는 시간 제한 간격을 설정할 수 있는 입니다 Split(String, String, RegexOptions, TimeSpan).

추가 정보

적용 대상

Split(String, Int32, Int32)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
Regex.Split.cs

입력 문자열을 Regex 생성자에서 지정된 정규식에 의해 정의된 위치에서 지정된 최대 수만큼 부분 문자열의 배열로 분할합니다. 입력 문자열에서 지정된 문자 위치부터 정규식 패턴을 검색합니다.

public:
 cli::array <System::String ^> ^ Split(System::String ^ input, int count, int startat);
public string[] Split (string input, int count, int startat);
member this.Split : string * int * int -> string[]
Public Function Split (input As String, count As Integer, startat As Integer) As String()

매개 변수

input
String

분할될 문자열입니다.

count
Int32

분할할 수 있는 최대 횟수입니다.

startat
Int32

입력 문자열에서 검색을 시작할 문자 위치입니다.

반환

String[]

문자열 배열입니다.

예외

input이(가) null인 경우

startat이 0보다 작거나 input의 길이보다 큰 경우

시간이 초과되었습니다. 시간 제한에 대한 자세한 내용은 설명 섹션을 참조하세요.

설명

메서드는 Regex.Split 문자열을 String.Split 문자 집합 대신 정규식에 의해 결정되는 구분 기호로 분할한다는 Regex.Split 점을 제외하고 메서드와 유사합니다. 매개 변수는 count 문자열이 분할되는 input 최대 부분 문자열 수를 지정합니다. 마지막 문자열에는 문자열의 unsplit 나머지가 포함됩니다. 값이 0이 count 면 가능한 한 여러 번 분할하는 기본 동작이 제공됩니다. 매개 변수는 startat 첫 번째 구분 기호에 대한 검색이 시작되는 지점을 정의합니다(선행 공백을 건너뛰는 데 사용할 수 있음).

에 대한 startat자세한 내용은 의 Match(String, Int32)설명 섹션을 참조하세요.

문자열의 count+1 위치에서 일치하는 항목을 찾을 수 없는 경우 메서드는 문자열을 포함하는 1개 요소 배열을 input 반환합니다. 하나 이상의 일치 항목이 발견되면 반환된 배열의 첫 번째 요소에는 일치 전의 첫 번째 문자부터 최대 한 문자까지 문자열의 첫 번째 부분이 포함됩니다.

여러 일치 항목이 서로 인접하고 찾은 일치 항목 수가 2개 미만 count인 경우 빈 문자열이 배열에 삽입됩니다. 마찬가지로 문자열의 첫 번째 문자인 에서 startat일치 항목이 발견되면 반환된 배열의 첫 번째 요소는 빈 문자열입니다. 즉, 인접 일치 항목에서 발생하는 빈 문자열은 일치하는 부분 문자열 수가 와 같은지 여부를 결정할 때 계산됩니다 count. 다음 예제에서는 정규식을 \d+ 사용하여 문자열에서 숫자 문자의 첫 번째 부분 문자열의 시작 위치를 찾은 다음 해당 위치에서 시작하여 문자열을 최대 3번 분할합니다. 정규식 패턴은 입력 문자열의 시작과 일치하므로 반환된 문자열 배열은 빈 문자열, 5자 알파벳 문자열 및 나머지 문자열로 구성됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJ789KLMNO012PQRST";
      Match m = rgx.Match(input);
      if (m.Success) { 
         int startAt = m.Index;
         string[] result = rgx.Split(input, 3, startAt);
         for (int ctr = 0; ctr < result.Length; ctr++) {
            Console.Write("'{0}'", result[ctr]);
            if (ctr < result.Length - 1)
               Console.Write(", ");
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       '', 'ABCDE', 'FGHIJKL789MNOPQ012'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim rgx As New Regex(pattern)
      Dim input As String = "123ABCDE456FGHIJ789KLMNO012PQRST"
      Dim m As Match = rgx.Match(input)
      If m.Success Then 
         Dim startAt As Integer = m.Index
         Dim result() As String = rgx.Split(input, 3, startAt)
         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 If
   End Sub               
End Module
' The example displays the following output:
'       '', 'ABCDE', 'FGHIJKL789MNOPQ012'

캡처 괄호를 정규식에 사용하는 경우 캡처된 텍스트는 분할 문자열 배열에 포함됩니다. 그러나 캡처된 텍스트가 포함된 배열 요소는 일치 항목 수가 에 도달 count했는지 여부를 결정할 때 계산되지 않습니다. 예를 들어 문자열 '"apple-apricot-plum-pear-pomegranate-pineapple-peach"를 문자열의 문자 15부터 시작하여 최대 4개의 부분 문자열로 분할하면 다음 코드와 같이 7개 요소 배열이 생성됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)";
      string input = "apple-apricot-plum-pear-pomegranate-pineapple-peach";

      // Split on hyphens from 15th character on
      Regex regex = new Regex(pattern);    
      // Split on hyphens from 15th character on
      string[] substrings = regex.Split(input, 4, 15);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// The method writes the following to the console:
//    'apple-apricot-plum'
//    '-'
//    'pear'
//    '-'
//    'pomegranate'
//    '-'
//    'pineapple-peach'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(-)"
      Dim input As String = "apple-apricot-plum-pear-pomegranate-pineapple-peach"

      Dim regex As Regex = New Regex(pattern)    
      ' Split on hyphens from 15th character on
      Dim substrings() As String = regex.Split(input, 4, 15)
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub  
End Module
' The example displays the following output:
'    'apple-apricot-plum'
'    '-'
'    'pear'
'    '-'
'    'pomegranate'
'    '-'
'    'pineapple-peach'

그러나 정규식 패턴에 여러 캡처 괄호 집합이 포함된 경우 이 메서드의 동작은 .NET Framework 버전에 따라 달라집니다. .NET Framework 1.0 및 1.1에서 첫 번째 캡처 괄호 집합 내에서 일치하는 항목을 찾을 수 없는 경우 추가 캡처 괄호에서 캡처한 텍스트는 반환된 배열에 포함되지 않습니다. .NET Framework 2.0부터 캡처된 모든 텍스트도 반환된 배열에 추가됩니다. 예를 들어 다음 코드는 두 세트의 캡처 괄호를 사용하여 문자열의 개별 단어를 추출합니다. 첫 번째 캡처 괄호 집합은 하이픈을 캡처하고 두 번째 집합은 세로 막대를 캡처합니다. 예제 코드가 컴파일되고 .NET Framework 1.0 또는 1.1에서 실행되는 경우 세로 막대 문자를 제외합니다. 컴파일되어 .NET Framework 2.0 이상 버전에서 실행되는 경우 해당 문자가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)|([|])";     // possible delimiters found in string
      string input = "apple|apricot|plum|pear|pomegranate|pineapple|peach";

      Regex regex = new Regex(pattern);    
      // Split on delimiters from 15th character on
      string[] substrings = regex.Split(input, 4, 15);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// In .NET 2.0 and later, the method returns an array of
// 7 elements, as follows:
//    apple|apricot|plum'
//    '|'
//    'pear'
//    '|'
//    'pomegranate'
//    '|'
//    'pineapple|peach'
// In .NET 1.0 and 1.1, the method returns an array of
// 4 elements, as follows:
//    'apple|apricot|plum'
//    'pear'
//    'pomegranate'
//    'pineapple|peach'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(-)|([|])"   ' possible delimiters found in string
      Dim input As String = "apple|apricot|plum|pear|pomegranate|pineapple|peach"

      Dim regex As Regex = New Regex(pattern)    
      ' Split on delimiters from 15th character on
      Dim substrings() As String = regex.Split(input, 4, 15)
      For Each match As String In substrings
         Console.WriteLine("'{0}'", match)
      Next
   End Sub
End Module
' In .NET 2.0, the method returns an array of
' 7 elements, as follows:
'    apple|apricot|plum'
'    '|'
'    'pear'
'    '|'
'    'pomegranate'
'    '|'
'    'pineapple|peach'
' In .NET 1.0 and 1.1, the method returns an array of
' 4 elements, as follows:
'    'apple|apricot|plum'
'    'pear'
'    'pomegranate'
'    'pineapple|peach'

정규식이 빈 문자열 Split 과 일치할 수 있는 경우 빈 문자열 구분 기호는 모든 위치에서 찾을 수 있으므로 문자열을 단일 문자열 배열로 분할합니다. 다음 예제에서는 문자열 "characters"를 입력 문자열에 포함된 만큼의 요소로 분할하여 문자 "a"로 시작합니다. null 문자열은 입력 문자열의 끝과 일치하므로 반환된 배열의 끝에 null 문자열이 삽입됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      Regex regex = new Regex("");
      string[] substrings = regex.Split(input, input.Length, input.IndexOf("a"));
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write(substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
// The example displays the following output:   
//    {, c, h, a, r, a, c, t, e, rs}
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "characters"
      Dim regex As New Regex("")
      Dim substrings() As String = regex.Split(input, input.Length, _
                                               input.IndexOf("a"))
      Console.Write("{")
      For ctr As Integer = 0 to substrings.Length - 1
         Console.Write(substrings(ctr))
         If ctr < substrings.Length - 1 Then Console.Write(", ")
      Next
      Console.WriteLine("}")
   End Sub
End Module
' The example produces the following output:   
'    {, c, h, a, r, a, c, t, e, rs}

RegexMatchTimeoutException 분할 작업의 실행 시간이 생성자가 지정한 제한 시간 간격을 초과하면 예외가 Regex.Regex(String, RegexOptions, TimeSpan) throw됩니다. 생성자를 호출할 때 시간 제한 간격을 설정 하지 않으면, 작업이 있는 애플리케이션 도메인에 설정 된 제한 시간 값을 초과 하면 예외가 throw 되는 Regex 개체가 만들어집니다. 제한 시간에 정의 된 경우는 Regex 생성자 호출 또는 애플리케이션 도메인의 속성 또는 시간 제한 값이 Regex.InfiniteMatchTimeout, 예외가 throw 되지 않습니다

추가 정보

적용 대상

Split(String, String, RegexOptions)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
Regex.Split.cs

지정된 정규식 패턴에 의해 정의된 위치에서 부분 문자열로 이루어진 배열로 입력 문자열을 분할합니다. 지정한 옵션에 따라 일치 작업이 수정됩니다.

public:
 static cli::array <System::String ^> ^ Split(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static string[] Split (string input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member Split : string * string * System.Text.RegularExpressions.RegexOptions -> string[]
Public Shared Function Split (input As String, pattern As String, options As RegexOptions) As String()

매개 변수

input
String

분할할 문자열입니다.

pattern
String

일치 항목을 찾을 정규식 패턴입니다.

options
RegexOptions

일치 옵션을 제공하는 열거형 값의 비트 조합입니다.

반환

String[]

문자열 배열입니다.

예외

정규식 구문 분석 오류가 발생했습니다.

input 또는 patternnull인 경우

optionsRegexOptions 값의 유효한 비트 조합이 아닙니다.

시간이 초과되었습니다. 시간 제한에 대한 자세한 내용은 설명 섹션을 참조하세요.

설명

메서드는 Regex.Split 문자열을 String.Split(Char[]) 문자 집합 대신 정규식에 의해 결정되는 구분 기호로 분할한다는 Regex.Split 점을 제외하고 메서드와 유사합니다. 문자열은 가능한 한 여러 번 분할됩니다. 구분 기호를 찾을 수 없는 경우 반환 값은 원래 input 문자열인 하나의 요소를 포함합니다.

매개 변수는 pattern 일치시킬 문자열을 상징적으로 설명하는 정규식 언어 요소로 구성됩니다. 정규식에 대한 자세한 내용은 .NET 정규식정규식 언어 - 빠른 참조를 참조하세요.

중요

정적 메서드 호출에 사용되는 컴파일된 정규 Split 식은 자동으로 캐시됩니다. 컴파일된 정규식의 수명을 직접 관리하려면 instance Split 메서드를 사용합니다.

여러 일치 항목이 서로 인접한 경우 빈 문자열이 배열에 삽입됩니다. 예를 들어 단일 하이픈에서 문자열을 분할하면 반환된 배열에 인접한 두 하이픈이 있는 위치에 빈 문자열이 포함됩니다.

입력 문자열의 시작 또는 끝에 일치하는 항목이 있으면 반환된 배열의 시작 또는 끝에 빈 문자열이 포함됩니다. 다음 예제에서는 정규식 패턴을 [a-z]+ 사용하여 대문자 또는 소문자 알파벳 문자에 대한 입력 문자열을 분할합니다. 문자열이 시작되고 일치하는 알파벳 문자로 끝나기 때문에 반환된 배열의 첫 번째 요소와 마지막 요소의 값은 입니다 String.Empty.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "[a-z]+";
      string input = "Abc1234Def5678Ghi9012Jklm";
      string[] result = Regex.Split(input, pattern, 
                                    RegexOptions.IgnoreCase);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', '1234', '5678', '9012', ''
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)
      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', ''

괄호 캡처가 식에 Regex.Split 사용되는 경우 캡처된 모든 텍스트가 결과 문자열 배열에 포함됩니다. 예를 들어 캡처하는 괄호 안에 배치된 하이픈에서 문자열 "plum-pear"를 분할하는 경우 반환된 배열에는 하이픈이 포함된 문자열 요소가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      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 example displays the following output:
//    'plum'
//    '-'
//    'pear'
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'

그러나 정규식 패턴에 여러 캡처 괄호 집합이 포함된 경우 이 메서드의 동작은 .NET Framework 버전에 따라 달라집니다. .NET Framework 1.0 및 1.1에서 첫 번째 캡처 괄호 집합 내에서 일치하는 항목을 찾을 수 없는 경우 추가 캡처 괄호에서 캡처한 텍스트는 반환된 배열에 포함되지 않습니다. .NET Framework 2.0부터 캡처된 모든 텍스트도 반환된 배열에 추가됩니다. 예를 들어 다음 코드는 두 세트의 캡처 괄호를 사용하여 날짜 문자열에서 날짜 구분 기호를 포함하여 날짜의 요소를 추출합니다. 첫 번째 캡처 괄호 집합은 하이픈을 캡처하고 두 번째 집합은 슬래시를 캡처합니다. 예제 코드가 컴파일되고 .NET Framework 1.0 또는 1.1에서 실행되는 경우 슬래시 문자를 제외합니다. 컴파일되어 .NET Framework 2.0 이상 버전에서 실행되는 경우 해당 문자가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      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 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
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'

정규식이 빈 문자열 Split 과 일치할 수 있는 경우 빈 문자열 구분 기호는 모든 위치에서 찾을 수 있으므로 문자열을 단일 문자열 배열로 분할합니다.

RegexMatchTimeoutException split 작업의 실행 시간이 메서드가 호출 되는 애플리케이션 도메인에 대 한 지정 된 시간 제한 간격을 초과 하는 경우 예외가 throw 됩니다. 시간 제한이 없으며 애플리케이션 도메인의 속성에 정의 된 제한 시간 값이 아니면 Regex.InfiniteMatchTimeout, 예외가 throw 되지 않습니다.

호출자 참고

이 메서드는 메서드가 호출 되는 애플리케이션 도메인의 기본 제한 시간 값에 해당 간격이 지난 후 시간이 합니다. 애플리케이션 도메인에 값에 대 한 제한 시간 값을 정의 되지 않은 경우 InfiniteMatchTimeout, 메서드 시간 초과 방지 하는는 데 사용 됩니다. 패턴 일치에서 텍스트를 분할하는 데 권장되는 정적 메서드는 시간 제한 간격을 설정할 수 있는 입니다 Split(String, String, RegexOptions, TimeSpan).

추가 정보

적용 대상

Split(String, String, RegexOptions, TimeSpan)

Source:
Regex.Split.cs
Source:
Regex.Split.cs
Source:
Regex.Split.cs

지정된 정규식 패턴에 의해 정의된 위치에서 부분 문자열로 이루어진 배열로 입력 문자열을 분할합니다. 추가 매개 변수는 일치하는 항목이 없는 경우 제한 시간 간격과 일치 작업을 수정하는 옵션을 지정합니다.

public:
 static cli::array <System::String ^> ^ Split(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static string[] Split (string input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member Split : string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> string[]
Public Shared Function Split (input As String, pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As String()

매개 변수

input
String

분할할 문자열입니다.

pattern
String

일치 항목을 찾을 정규식 패턴입니다.

options
RegexOptions

일치 옵션을 제공하는 열거형 값의 비트 조합입니다.

matchTimeout
TimeSpan

시간 제한 간격이거나, 메서드에 시간 제한이 없어야 함을 나타내는 InfiniteMatchTimeout 입니다.

반환

String[]

문자열 배열입니다.

예외

정규식 구문 분석 오류가 발생했습니다.

input 또는 patternnull인 경우

optionsRegexOptions 값의 유효한 비트 조합이 아닙니다.

또는

matchTimeout 이 음수, 0 또는 약 24일보다 큽니다.

시간이 초과되었습니다. 시간 제한에 대한 자세한 내용은 설명 섹션을 참조하세요.

설명

메서드는 Regex.Split 문자열을 String.Split(Char[]) 문자 집합 대신 정규식에 의해 결정되는 구분 기호로 분할한다는 Regex.Split 점을 제외하고 메서드와 비슷합니다. 문자열은 가능한 한 여러 번 분할됩니다. 구분 기호를 찾을 수 없는 경우 반환 값에는 원래 문자열 값이 있는 요소가 하나 포함됩니다 input .

매개 변수는 pattern 일치시킬 문자열을 상징적으로 설명하는 정규식 언어 요소로 구성됩니다. 정규식에 대한 자세한 내용은 .NET 정규식정규식 언어 - 빠른 참조를 참조하세요.

중요

정적 Split 메서드 호출에 사용되는 컴파일된 정규식은 자동으로 캐시됩니다. 컴파일된 정규식의 수명을 직접 관리하려면 instance Split 메서드를 사용합니다.

여러 일치 항목이 서로 인접한 경우 빈 문자열이 배열에 삽입됩니다. 예를 들어 단일 하이픈에서 문자열을 분할하면 반환된 배열에 인접한 두 하이픈이 있는 위치에 빈 문자열이 포함됩니다.

입력 문자열의 시작 또는 끝에 일치하는 항목이 있으면 반환된 배열의 시작 또는 끝에 빈 문자열이 포함됩니다. 다음 예제에서는 정규식 패턴을 [a-z]+ 사용하여 대문자 또는 소문자 알파벳 문자에 대한 입력 문자열을 분할합니다. 문자열이 시작되고 일치하는 알파벳 문자로 끝나기 때문에 반환된 배열의 첫 번째 및 마지막 요소 값은 입니다 String.Empty.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "[a-z]+";
      string input = "Abc1234Def5678Ghi9012Jklm";
      string[] result = Regex.Split(input, pattern, 
                                    RegexOptions.IgnoreCase,
                                    TimeSpan.FromMilliseconds(500));
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       '', '1234', '5678', '9012', ''
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', ''

식에 캡처 괄호를 Regex.Split 사용하는 경우 캡처된 모든 텍스트가 결과 문자열 배열에 포함됩니다. 예를 들어 캡처하는 괄호 안에 배치된 하이픈에서 문자열 "plum-pear"를 분할하는 경우 반환된 배열에는 하이픈이 포함된 문자열 요소가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      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 example displays the following output:
//    'plum'
//    '-'
//    'pear'
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'

그러나 정규식 패턴에 여러 캡처 괄호 집합이 포함된 경우 이 메서드의 동작은 .NET Framework 버전에 따라 달라집니다. .NET Framework 1.0 및 1.1에서 첫 번째 캡처 괄호 집합 내에서 일치 항목을 찾을 수 없는 경우 추가 캡처 괄호에서 캡처된 텍스트는 반환된 배열에 포함되지 않습니다. .NET Framework 2.0부터 캡처된 모든 텍스트도 반환된 배열에 추가됩니다. 예를 들어 다음 코드는 두 개의 캡처 괄호 집합을 사용하여 날짜 문자열에서 날짜 구분 기호를 포함하여 날짜의 요소를 추출합니다. 첫 번째 캡처 괄호 집합은 하이픈을 캡처하고 두 번째 집합은 슬래시를 캡처합니다. 예제 코드가 컴파일되어 .NET Framework 1.0 또는 1.1에서 실행되는 경우 슬래시 문자는 제외됩니다. 컴파일되어 .NET Framework 2.0 이상 버전에서 실행되는 경우 해당 문자가 포함됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      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 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
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'

정규식이 빈 문자열 Split 과 일치할 수 있는 경우 는 빈 문자열 구분 기호를 모든 위치에서 찾을 수 있으므로 문자열을 단일 문자열 배열로 분할합니다.

매개 변수는 matchTimeout 패턴 일치 메서드가 시간 초과되기 전에 일치 항목을 찾으려고 시도하는 기간을 지정합니다. 제한 시간 간격을 설정하면 거의 일치하는 항목이 포함된 입력을 처리할 때 과도한 역추적에 의존하는 정규식이 응답을 중지하는 것처럼 보이지 않습니다. 자세한 내용은 정규식 및 역추적에 대한 모범 사례를 참조하세요. 해당 시간 간격에 일치하는 항목이 없으면 메서드는 예외를 RegexMatchTimeoutException throw합니다. matchTimeout 메서드를 실행 하는 애플리케이션 도메인에 대해 정의 된 모든 기본 제한 시간 값을 재정의 합니다.

호출자 참고

매개 변수를 matchTimeout 2초와 같은 적절한 값으로 설정하는 것이 좋습니다. 를 지정하여 시간 초과를 InfiniteMatchTimeout사용하지 않도록 설정하면 정규식 엔진이 약간 더 나은 성능을 제공합니다. 그러나 다음 조건에서만 시간 초과를 사용하지 않도록 설정해야 합니다.

  • 정규식에서 처리된 입력이 알려진 신뢰할 수 있는 원본에서 파생되거나 정적 텍스트로 구성된 경우 이렇게 하면 사용자가 동적으로 입력한 텍스트가 제외됩니다.

  • 정규식 패턴을 철저히 테스트하여 일치 항목, 비 일치 항목 및 근거리 일치를 효율적으로 처리하는지 확인합니다.

  • 정규식 패턴에 거의 일치 항목을 처리할 때 과도한 역추적을 유발하는 것으로 알려진 언어 요소가 없는 경우

추가 정보

적용 대상