Home  >  Article  >  Backend Development  >  What is the Regex class and its class methods in C#?

What is the Regex class and its class methods in C#?

王林
王林forward
2023-08-31 10:45:101050browse

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

The Regex class is used to represent regular expressions. Regular expressions are patterns that match input text.

The following are the methods of the Regex class-

Teacher ID Methods and instructions
1 public bool IsMatch(string input)

Indicates whether the specified regular expression Regex constructor is in the specified input string Find matches.

2 public bool IsMatch(string input, int startat)

Indicates Regex construction Whether the regular expression specified in the function finds a match in the specified input string, starting at the specified starting position in the string.

3 public static bool IsMatch(string input, string mode)

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

4 public MatchCollection Matches (string input)

Input characters specified Search a string for all occurrences of a regular expression.

td>

5 Public String Replacement (String Input, String Replacement)

In the specified input string, replaces all strings matching the regular expression pattern with the specified replacement string.

6 public string[] Split(string input)

Will input string Split into an array of substrings whose positions are defined by the regular expression pattern specified in the Regex constructor.

The following example uses Matches() Method searches the specified input string -

Example

Live demonstration

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();
      }
   }
}

Output

Matching words start with 'm' and ends with 'e':
The Expression: \bm\S*e\b
make
maze
manage
measure

The above is the detailed content of What is the Regex class and its class methods in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete