工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> C言語

C# - 正規表現

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

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

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

正規表現クラス

Regex クラスは、正規表現を表すために使用されます。次の一般的に使用されるメソッドがあります-

Sr.No. 方法と説明
1

public bool IsMatch(文字列入力)

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

2

public bool IsMatch(string input, int startat)

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

3

public static bool IsMatch(string input, string pattern)

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

4

公開 MatchCollection マッチ (文字列入力)

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

5

public string Replace(文字列入力、文字列置換)

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

6

public string[] Split(文字列入力)

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   

C言語

  1. C# 式、ステートメント、およびブロック (例付き)
  2. C# 文字列
  3. Java 式、ステートメント、およびブロック
  4. Java 文字列
  5. Java列挙型文字列
  6. Java ラムダ式
  7. C++ 文字列:strcpy()、strcat()、strlen()、strcmp() 例
  8. C の文字列:変数の宣言、初期化、印刷、例の方法
  9. 例を使用した Python 文字列 count()
  10. Java-文字列クラス
  11. Java-正規表現