卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16898本站已运行3322

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

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

Regex 类用于表示正则表达式。正则表达式是可以与输入文本进行匹配的模式。

以下是 Regex 类的方法 -

< tr>
老师号方法及说明
1public bool IsMatch(string input)

表示是否指定的正则表达式Regex 构造函数在指定的输入字符串中查找匹配项。

2public bool IsMatch(string input, int startat)

指示 Regex 构造函数中指定的正则表达式是否在指定输入字符串中从字符串中指定的起始位置开始找到匹配项。< /p>

3public static bool IsMatch(字符串输入,字符串模式)

指示指定的正则表达式是否在指定的输入字符串中找到匹配项。

4 public MatchCollection Matches(字符串输入)

在指定的输入字符串中搜索所有出现的正则表达式。

5公共字符串替换(字符串输入,字符串替换)

In指定的输入字符串,用指定的替换字符串替换与正则表达式模式匹配的所有字符串。

6public 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

卓越飞翔博客
上一篇: 掌握 Laravel 中间件的基础知识
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏