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

王林
发布: 2023-08-31 10:45:10
转载
1061人浏览过

c# 中的 regex 类及其类方法是什么?

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

以下是 Regex 类的方法 -

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

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

2 public bool IsMatch(string input, int startat)

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

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

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

4 public MatchCollection Matches(字符串输入)

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

td>

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

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

6 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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号