Regex クラスとは? わかりやすく解説

Regex クラス

変更不可正規表現表します

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

<SerializableAttribute> _
Public Class Regex
    Implements ISerializable
[SerializableAttribute] 
public class Regex : ISerializable
[SerializableAttribute] 
public ref class Regex : ISerializable
/** @attribute SerializableAttribute() */ 
public class Regex implements ISerializable
SerializableAttribute 
public class Regex implements ISerializable
解説解説
使用例使用例

正規表現使用して文字列通貨値を表す正し形式かどうかチェックするコード例次に示します^ および $ トークンで囲むことにより、部分文字列ではなく文字列全体正規表現一致する必要があることを示している点に注意してください

using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main
 ()
    {

          // Define a regular expression for currency values.
          Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
          
          // Define some test strings.
          string[] tests = {"-42", "19.99",
 "0.001", "100 USD"};
          
          // Check each test string against the regular expression.
          foreach (string test in
 tests)
          {
              if (rx.IsMatch(test))
              {
                  Console.WriteLine("{0} is a currency value.", test);
              }
              else
              {
                  Console.WriteLine("{0} is not a currency value.", test);
              }
          }
         
    }    
    
}
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
int main()
{
   
   // Define a regular expression for currency values.
   Regex^ rx = gcnew Regex( "^-?\\d+(\\.\\d{2})?$" );
   
   // Define some test strings.
   array<String^>^tests = {"-42","19.99","0.001","100
 USD"};
   
   // Check each test string against the regular expression.
   System::Collections::IEnumerator^ myEnum = tests->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ test = safe_cast<String^>(myEnum->Current);
      if ( rx->IsMatch( test ) )
      {
         Console::WriteLine( "{0} is a currency value.", test );
      }
      else
      {
         Console::WriteLine( "{0} is not a currency value.", test );
      }
   }
}

import System.*;
import System.Text.RegularExpressions.*;

public class Test
{
    public static void main(String[]
 args)
    {
        // Define a regular expression for currency values.
        Regex rx = new Regex("^-?\\d+(\\.\\d{2})?$");

        // Define some test strings.
        String tests[] =  { "-42", "19.99", "0.001",
 "100 USD" };

        // Check each test string against the regular expression.
        for (int iCtr = 0; iCtr < tests.get_Length();
 iCtr++) {
            String test = (String)tests.get_Item(iCtr);
            if (rx.IsMatch(test)) {
                Console.WriteLine("{0} is a currency value.", test);
            }
            else {
                Console.WriteLine("{0} is not a currency value.", test);
            }
        }
    } //main 
} //Test

正規表現使用して文字列内で繰り返し出現する語をチェックするコード例次に示します(?<word>) 構成体を使用してグループに名前を付け(\k<word>) 構成体を使用してそのグループ式内で後から参照していることに注意してください

using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main
 ()
    {

        // Define a regular expression for repeated words.
        Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b"
,
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.        
        string text = "The the quick brown fox  fox jumped
 over the lazy dog dog.";
        
        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found.", matches.Count);

        // Report on each match.
        foreach (Match match in matches)
        {
            string word = match.Groups["word"].Value;
            int index = match.Index;
            Console.WriteLine("{0} repeated at position {1}", word, index);
   
        }
        
    }
    
}
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
int main()
{
   // Define a regular expression for repeated words.
   Regex^ rx = gcnew Regex( "\\b(?<word>\\w+)\\s+(\\k<word>)\\b",static_cast<RegexOptions>(RegexOptions::Compiled
 | RegexOptions::IgnoreCase) );

   // Define a test string.        
   String^ text = "The the quick brown fox  fox jumped over the lazy dog dog.";

   // Find matches.
   MatchCollection^ matches = rx->Matches( text );

   // Report the number of matches found.
   Console::WriteLine( "{0} matches found.", matches->Count );

   // Report on each match.
   for each (Match^ match in matches)
   {
      String^ word = match->Groups["word"]->Value;
      int index = match->Index;
      Console::WriteLine("{0} repeated at position {1}", word, index);
   
   }
}
import System.*;
import System.Text.RegularExpressions.*;

public class Test
{
    public static void main(String[]
 args)
    {
        // Define a regular expression for repeated words.
        Regex rx = new Regex("\\b(?<word>\\w+)\\s+(\\k<word>)\\b",
 
            RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.        
        String text = "The the quick brown fox  fox jumped over the "
            + "lazy dog dog.";

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found.", (Int32)matches.get_Count());

        // Report on each match.
        for (int iCtr = 0; iCtr < matches.get_Count();
 iCtr++) {
            Match match = matches.get_Item(iCtr);
            String word = match.get_Groups().get_Item("word").get_Value();
            int index = match.get_Index();
            Console.WriteLine("{0} repeated at position {1}", word, 
                (Int32)index);
        }
    } //main       
} //Test
継承階層継承階層
System.Object
  System.Text.RegularExpressions.Regex
     派生クラス
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Regex クラス」の関連用語

Regex クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Regex クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS