Regex.GetGroupNames 메서드

정의

정규식에 대한 캡처링 그룹 이름의 배열을 반환합니다.

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

반환

String[]

그룹 이름의 문자열 배열입니다.

예제

다음 예제에서는 정규식 그룹의 이름과 일치하는 텍스트를 표시하는 범용 ShowMatches 메서드를 정의합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po})";
      string input = "The cow jumped over the moon.";
      Regex rgx = new Regex(pattern);
      Match match = rgx.Match(input);
      if (match.Success)
         ShowMatches(rgx, match);
   }

   private static void ShowMatches(Regex r, Match m)
   {
      string[] names = r.GetGroupNames();
      Console.WriteLine("Named Groups:");
      foreach (var name in names) {
         Group grp = m.Groups[name];
         Console.WriteLine("   {0}: '{1}'", name, grp.Value);
      }
   }
}
// The example displays the following output:
//       Named Groups:
//          0: 'The cow jumped over the moon.'
//          1: 'the '
//          2: 'the'
//          FirstWord: 'The'
//          LastWord: 'moon'
//          Punctuation: '.'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po})"
      Dim input As String = "The cow jumped over the moon."
      Dim rgx As New Regex(pattern)
      Dim match As Match = rgx.Match(input)
      If match.Success Then ShowMatches(rgx, match)
   End Sub
   
   Private Sub ShowMatches(r As Regex, m As Match)
      Dim names() As String = r.GetGroupNames()
      Console.WriteLine("Named Groups:")
      For Each name In names
         Dim grp As Group = m.Groups.Item(name)
         Console.WriteLine("   {0}: '{1}'", name, grp.Value)
      Next
   End Sub
End Module
' The example displays the following output:
'       Named Groups:
'          0: 'The cow jumped over the moon.'
'          1: 'the '
'          2: 'the'
'          FirstWord: 'The'
'          LastWord: 'moon'
'          Punctuation: '.'

이 경우 정규식 패턴 \b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po}) 은 간단한 문장을 구문 분석하고 첫 번째 단어, 마지막 단어 및 끝 문장 부호를 식별하기 위한 것입니다. 다음 표에서는 정규식 패턴이 해석되는 방법을 보여 줍니다.

무늬 설명
\b 단어 경계에서 일치 항목 찾기를 시작합니다.
(?<FirstWord>\w+) 하나 이상의 단어 문자를 찾습니다. 명명된 FirstWord 그룹입니다.
\s? 0회 이상 나오는 공백 문자를 찾습니다.
(\w+) 하나 이상의 단어 문자를 찾습니다. 이 그룹은 두 번째 캡처링 그룹입니다.
\s 공백 문자를 찾습니다.
((\w+)\s)* 공백 뒤에 하나 이상의 단어 문자가 0개 이상 나타나는 것을 찾습니다. 이 그룹은 첫 번째 캡처링 그룹입니다.
(?<LastWord> \w+)? 하나 이상의 단어 문자를 0개 또는 한 개 이상 찾습니다. 명명된 LastWord 그룹입니다.
(?<Punctuation> \p{Po}) 유니코드 범주가 문장 부호인 문자와 일치합니다. 명명된 Punctuation 그룹입니다.

설명

그룹 이름 컬렉션에는 식에서 캡처링 그룹의 이름을 지정하는 데 사용되는 문자열 집합이 포함되어 있습니다. 캡처링 그룹의 이름이 명시적으로 지정되지 않더라도 숫자 이름("0", "1", "2", "3" 등)이 자동으로 할당됩니다. 명명된 "0" 그룹은 정규식 패턴과 일치하는 모든 텍스트를 나타냅니다. 번호가 매겨진 그룹은 컬렉션에서 명시적으로 명명된 그룹 앞에 있으며 명명된 그룹은 정규식 패턴에서 정의된 순서대로 표시됩니다.

이 메서드에서 반환된 Length 배열의 속성을 사용하여 정규식의 그룹 수를 확인할 수 있습니다.

적용 대상

추가 정보