次の方法で共有


方法: 正規表現を使用して文字列を解析する (C++/CLI)

System.Text.RegularExpressions 名前空間の Regex クラスを使用して単純な文字列を解析する方法を次のコード例に示します。複数のワード デリニエイタの型を含む文字列が構成されます。次に、Regex クラスを Match クラスと共に使用して文字列が解析されます。さらに、センテンス内の各単語を個別に表示します。

使用例

// 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 の正規表現

Visual C++ での .NET プログラミング