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

CsharpProgrammingServer Side Programming

The Regex class is used for representing a regular expression. A regular expression is a pattern that could be matched against an input text.

The following are the methods of Regex class −

Sr.NoMethod & Description
1public bool IsMatch(string input)
Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.
2public 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.
3public static bool IsMatch(string input, string pattern)
Indicates whether the specified regular expression finds a match in the specified input string.
4public MatchCollection Matches(string input)
Searches the specified input string for all occurrences of a regular expression.
5public 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.
6public 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.

The following example use the Matches() method to search the specified input string −

Example

 Live Demo

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
raja
Published on 09-Aug-2018 11:32:31
Advertisements