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

王林
Release: 2023-08-31 10:45:10
forward
1081 people have browsed it

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

Output

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template