C#正则表达式

/ / C#正则表达式

正则表达式是一种可以与输入文本匹配的模式。

Regex类

Regex类用于表示正则表达式。它有以下常用方法-

Sr.No. Methods & 描述
1

public bool IsMatch(string input)

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

2

public bool IsMatch(string input,int startat)

指定的正则表达式是否在指定的input字符串中找到匹配项,从字符串中的指定startat起始位置开始。

3

public static bool IsMatch(string input,string pattern)

指定的正则表达式是否与input字符串找到匹配。

4

public MatchCollection Matches(string input)

指定的字符串中搜索正则表达式的所有匹配项。

5

public string Replace(string input,string replacement)

无涯教程网

用指定的替换字符串替换与正则表达式模式匹配的字符串。

6

public string[] split(String Input)

将字符串拆分为子字符串数组。

以下示例匹配以"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

以下示例匹配以‘m'开头并以‘e'-结尾的单词

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-regular-expressions.html

来源:LearnFk无涯教程网

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

此下示例替换了额外的空白-

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   

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

Rust并发编程实战课 -〔晁岳攀(鸟窝)〕

RAG在企业数智化场景下的设计与改进 -〔张颖峰〕

Stable Diffusion实战课​ -〔夜月 ​& 散步​〕

重学TypeScript -〔周爱民〕

Vue 3 企业级项目实战课 -〔杨文坚〕

Flink核心技术与实战 -〔张利兵〕

罗剑锋的C++实战笔记 -〔罗剑锋〕

分布式技术原理与算法解析 -〔聂鹏程〕

从0开始学架构 -〔李运华〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者