Csharp-regular-expressions

提供:Dev Guides
移動先:案内検索

C#-正規表現

  • 正規表現*は、入力テキストと照合できるパターンです。 .Netフレームワークは、このようなマッチングを可能にする正規表現エンジンを提供します。 パターンは、1つ以上の文字リテラル、演算子、または構造で構成されます。

正規表現を定義するための構成

正規表現を定義できる文字、演算子、および構造のさまざまなカテゴリがあります。 次のリンクをクリックして、これらの構造を見つけます。

  • リンク:/csharp/csharp_character_escapes [文字エスケープ]
  • リンク:/csharp/csharp_character_classes [文字クラス]
  • リンク:/csharp/csharp_anchors [アンカー]
  • リンク:/csharp/csharp_grouping_constructs [グループ化構成]
  • リンク:/csharp/csharp_quantifiers [Quantifiers]
  • リンク:/csharp/csharp_backreference_constructs [バックリファレンスコンストラクト]
  • リンク:/csharp/csharp_alternation_constructs [代替構成]
  • リンク:/csharp/csharp_substitutions [Substitutions] *リンク:/csharp/csharp_miscellaneous_constructs [その他の構成]

正規表現クラス

Regexクラスは、正規表現を表すために使用されます。 以下の一般的に使用される方法があります-

Sr.No. Methods & Description
1
  • public bool IsMatch(string input)*

Regexコンストラクターで指定された正規表現が、指定された入力文字列で一致を見つけるかどうかを示します。

2

public bool IsMatch(string input, int startat)

Regexコンストラクターで指定された正規表現が、文字列の指定された開始位置から始まる、指定された入力文字列で一致を見つけるかどうかを示します。

3

public static bool IsMatch(string input, string pattern)

指定された正規表現が指定された入力文字列で一致を見つけるかどうかを示します。

4

public MatchCollection Matches(string input)

正規表現のすべての出現について、指定された入力文字列を検索します。

5

public string Replace(string input, string replacement)

指定された入力文字列で、正規表現パターンに一致するすべての文字列を指定された置換文字列に置き換えます。

6

public string[] Split(string input)

Regexコンストラクターで指定された正規表現パターンで定義された位置で、入力文字列を部分文字列の配列に分割します。

メソッドとプロパティの完全なリストについては、C#のMicrosoftドキュメントをお読みください。

例1

次の例では、「S」で始まる単語に一致します-

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);

         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      static void Main(string[] args) {
         string str = "A Thousand Splendid Suns";

         Console.WriteLine("Matching words that start with 'S': ");
         showMatch(str, @"\bS\S*");
         Console.ReadKey();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Matching words that start with 'S':
The Expression: \bS\S*
Splendid
Suns

例2

次の例では、「m」で始まり「e」で終わる単語に一致します-

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);

         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      static void Main(string[] args) {
         string str = "make maze and manage to measure it";

         Console.WriteLine("Matching words start with 'm' and ends with 'e':");
         showMatch(str, @"\bm\S*e\b");
         Console.ReadKey();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Matching words start with 'm' and ends with 'e':
The Expression: \bm\S*e\b
make
maze
manage
measure

実施例3

この例では、余分な空白を置き換えます-

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      static void Main(string[] args) {
         string input = "Hello   World   ";
         string pattern = "\\s+";
         string replacement = " ";

         Regex rgx = new Regex(pattern);
         string result = rgx.Replace(input, replacement);

         Console.WriteLine("Original String: {0}", input);
         Console.WriteLine("Replacement String: {0}", result);
         Console.ReadKey();
      }
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Original String: Hello World
Replacement String: Hello World