방법: 정규식을 사용하여 문자열 구문 분석

업데이트: 2007년 11월

다음 코드 예제에서는 System.Text.RegularExpressions 네임스페이스의 Regex 클래스를 사용하여 간단한 문자열을 구문 분석하는 방법을 보여 줍니다. 여러 형식의 단어 설명자가 포함된 문자열이 생성됩니다. 이 문자열은 Match 클래스와 함께 Regex 클래스를 사용하여 구문 분석됩니다. 그런 다음 문장의 각 단어가 개별적으로 표시됩니다.

예제

// regex_parse.cpp
// compile with: /clr
#using <system.dll>

using namespace System;
using namespace System::Text::RegularExpressions;

int main( )
{
   int words = 0;
   String^ pattern = "[a-zA-Z]*";
   Console::WriteLine( "pattern : '{0}'", pattern );
   Regex^ regex = gcnew Regex( pattern );

   String^ line = "one\ttwo three:four,five six  seven";   
   Console::WriteLine( "text : '{0}'", line );
   for( Match^ match = regex->Match( line ); 
        match->Success; match = match->NextMatch( ) ) 
   {
      if( match->Value->Length > 0 )
      {
         words++;
         Console::WriteLine( "{0}", match->Value );
      }
   }
   Console::WriteLine( "Number of Words : {0}", words );

   return 0;
}

참고 항목

기타 리소스

.NET Framework 정규식

.NET 프로그래밍 가이드