首頁  >  文章  >  後端開發  >  C# 中的 Regex 類別及其類別方法是什麼?

C# 中的 Regex 類別及其類別方法是什麼?

王林
王林轉載
2023-08-31 10:45:101050瀏覽

C# 中的 Regex 类及其类方法是什么?

Regex 類別用來表示正規表示式。正規表示式是可以與輸入文字進行匹配的模式。

以下是Regex 類別的方法-

34 56
老師號碼 方法及說明
1 public bool IsMatch(string input)

表示是否指定的正規表示式Regex 建構函式在指定的輸入字串中尋找匹配項。

2 public bool IsMatch(string input, int startat)##指示Regex 構造函數中指定的正規表示式是否在指定輸入字串中從字串中指定的起始位置開始找到匹配項。

public static bool IsMatch(字串輸入,字串模式)指示指定的正規表示式是否在指定的輸入字串中找到匹配項。

public MatchCollection Matches(字串輸入)在指定的輸入字符在字串中搜尋所有出現的正規表示式。

td>

公有字串替換(字串輸入,字串替換) In指定的輸入字串,用指定的替換字串替換與正規表示式模式相符的所有字串。

public string[] Split(string input)將輸入字串拆分為子字串數組,其位置由Regex 建構函數中指定的正規表示式模式定義.

以下範例使用Matches()方法搜尋指定的輸入字串-

範例

 現場示範

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

以上是C# 中的 Regex 類別及其類別方法是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除