Regex.GetGroupNumbers 메서드

정의

배열 내의 그룹 이름에 해당하는 캡처링 그룹 번호의 배열을 반환합니다.

public:
 cli::array <int> ^ GetGroupNumbers();
public int[] GetGroupNumbers ();
member this.GetGroupNumbers : unit -> int[]
Public Function GetGroupNumbers () As Integer()

반환

Int32[]

그룹 번호의 정수 배열입니다.

예제

다음 예제에서는 문장과 일치하는 정규식을 \b((?<word>\w+)\s*)+(?<end>[.?!])정의합니다. 정규식에는 세 개의 캡처링 그룹이 포함됩니다. 즉, 개별 단어를 캡처하는 명명되지 않은 그룹과 그 뒤에 있을 수 있는 공백 문자가 포함됩니다. 문장의 개별 단어를 캡처하는 그룹 word 및 문장을 끝내는 문장 부호를 캡처하는 그룹 end 입니다. 이 예제에서는 메서드를 GetGroupNumbers 호출하여 모든 캡처링 그룹의 수를 가져옵니다. 그런 다음 캡처된 문자열을 표시합니다. 또한 GroupNameFromNumber 이 메서드는 번호가 매겨진 특정 그룹이 명명된 그룹에 해당하는지 여부를 나타내는 데 사용됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b((?<word>\w+)\s*)+(?<end>[.?!])";
      string input = "This is a sentence. This is a second sentence.";
      
      Regex rgx = new Regex(pattern);
      int[] groupNumbers = rgx.GetGroupNumbers();
      Match m = rgx.Match(input);
      if (m.Success) {
         Console.WriteLine("Match: {0}", m.Value);
         foreach (var groupNumber in groupNumbers) {
            string name = rgx.GroupNameFromNumber(groupNumber);
            int number;
            Console.WriteLine("   Group {0}{1}: '{2}'", 
                              groupNumber, 
                              ! string.IsNullOrEmpty(name) & 
                              ! Int32.TryParse(name, out number) ?
                                 " (" + name + ")" : String.Empty, 
                              m.Groups[groupNumber].Value);
         }
      } 
   }
}
// The example displays the following output:
//       Match: This is a sentence.
//          Group 0: 'This is a sentence.'
//          Group 1: 'sentence'
//          Group 2 (word): 'sentence'
//          Group 3 (end): '.'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String= "\b((?<word>\w+)\s*)+(?<end>[.?!])"
      Dim input As String = "This is a sentence. This is a second sentence."
      
      Dim rgx As New Regex(pattern)
      Dim groupNumbers() As Integer = rgx.GetGroupNumbers()
      Dim m As Match = rgx.Match(input)
      If m.Success Then
         Console.WriteLine("Match: {0}", m.Value)
         For Each groupNumber In groupNumbers
            Dim name As String = rgx.GroupNameFromNumber(groupNumber)
            Dim number As Integer
            Console.WriteLine("   Group {0}{1}: '{2}'", 
                              groupNumber, 
                              If(Not String.IsNullOrEmpty(name) And 
                              Not Int32.TryParse(name, number),
                                 " (" + name + ")", String.Empty), 
                              m.Groups(groupNumber).Value)
         Next
      End If 
   End Sub
End Module
' The example displays the following output:
'       Match: This is a sentence.
'          Group 0: 'This is a sentence.'
'          Group 1: 'sentence'
'          Group 2 (word): 'sentence'
'          Group 3 (end): '.'

정규식 패턴은 다음 테이블과 같이 해석됩니다.

무늬 설명
\b 단어 경계에서 일치 항목 찾기를 시작합니다.
(?<word>\w+) 하나 이상의 단어 문자를 일치시키고 일치하는 문자열을 이름이 지정된 word그룹에 할당합니다.
\s* 0개 이상의 공백 문자가 일치하는지 확인합니다.
((?<word>\w+)\s*) word 캡처된 그룹 뒤에 캡처된 공백 문자를 첫 번째 캡처된 그룹에 할당합니다.
((?<word>\w+)\s*)+ 하나 이상의 단어 문자 패턴과 공백 문자를 한 번 이상 찾습니다.
(?<end>[.?!]) 마침표, 물음표 또는 느낌표를 찾습니다. 일치하는 문자를 end 캡처링 그룹에 할당합니다.

설명

명명되지 않은 캡처링 그룹과 명명된 캡처링 그룹 모두 숫자로 액세스할 수 있습니다. 명명되지 않은 그룹은 1부터 왼쪽에서 오른쪽으로 번호가 매겨집니다. 인덱스 0(0)의 캡처링 그룹은 일치 항목을 전체적으로 나타냅니다. 명명된 그룹은 명명되지 않은 캡처링 그룹의 수보다 큰 숫자로 시작하여 왼쪽에서 오른쪽으로 번호가 매겨집니다.

문자열 이름 대신 해당 숫자로 그룹을 참조하면 액세스 속도가 빨라질 수 있습니다.

적용 대상

추가 정보