次の方法で共有


方法: 正規表現を使用して単純検索を行う (C++/CLI)

正規表現を使用して部分文字列の完全一致を検索するコード例を次に示します。この検索は、静的な IsMatch メソッドで実行されます。このメソッドは、入力として 2 つの文字列を受け取ります。1 つ目は検索文字列で、2 つ目は、検索パターンです。

使用例

// regex_simple.cpp
// compile with: /clr
#using <System.dll>

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

int main()
{
   array<String^>^ sentence = 
   {
      "cow over the moon",
      "Betsy the Cow",
      "cowering in the corner",
      "no match here"
   };
    
   String^ matchStr = "cow";
   for (int i=0; i<sentence->Length; i++)
   {
      Console::Write( "{0,24}", sentence[i] );
      if ( Regex::IsMatch( sentence[i], matchStr,
                     RegexOptions::IgnoreCase ) )
         Console::WriteLine("  (match for '{0}' found)", matchStr);
      else
         Console::WriteLine("");
   }
   return 0;
}

参照

その他の技術情報

.NET Framework の正規表現

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