- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Regex class and its class methods in C#?
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.No | Method & 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. |
The following example use the Matches() method to search the specified input string −
Example
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
Advertisements