HOW TO:使用規則運算式進行簡單對應 (C++/CLI)

下列程式碼範例會使用規則運算式 (Regular Expression) 尋找完全相符的子字串。搜尋會使用靜態 IsMatch 方法進行,此方法取用兩個字串做為輸入。第一個是要進行搜尋的字串,第二個是要搜尋的模式。

範例

// 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 規則運算式

.NET 程式設計的 Visual C++