📜  C#-正则表达式

📅  最后修改于: 2020-12-28 05:10:53             🧑  作者: Mango


正则表达式是可以与输入文本匹配的模式。 .Net框架提供了允许此类匹配的正则表达式引擎。模式由一个或多个字符字面量,运算符或构造组成。

定义正则表达式的构造

字符,运算符和构造有多种类别,可让您定义正则表达式。单击以下链接以找到这些构造。

正则表达式类

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

Sr.No. Methods & Description
1

public bool IsMatch(string input)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.

2

public bool IsMatch(string input, int startat)

Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.

3

public static bool IsMatch(string input, string pattern)

Indicates whether the specified regular expression finds a match in the specified input string.

4

public MatchCollection Matches(string input)

Searches the specified input string for all occurrences of a regular expression.

5

public string Replace(string input, string replacement)

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.

6

public string[] Split(string input)

Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.

有关方法和属性的完整列表,请阅读有关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