- C# - File I/O
- C# - Exception Handling
- C# - Regular Expressions
- C# - Preprocessor Directives
- C# - Namespaces
- C# - Interfaces
- C# - Operator Overloading
- C# - Polymorphism
- C# - Inheritance
- C# - Classes
- C# - Enums
- C# - Structure
- C# - Strings
- C# - Arrays
- C# - Nullables
- C# - Methods
- C# - Encapsulation
- C# - Loops
- C# - Decision Making
- C# - Operators
- C# - Constants
- C# - Variables
- C# - Type Conversion
- C# - Data Types
- C# - Basic Syntax
- C# - Program Structure
- C# - Environment
- C# - Overview
- C# - Home
C# Advanced Tutorial
- C# - Multithreading
- C# - Unsafe Codes
- C# - Anonymous Methods
- C# - Generics
- C# - Collections
- C# - Events
- C# - Delegates
- C# - Indexers
- C# - Properties
- C# - Reflection
- C# - Attributes
C# Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C# - Regular Expressions
A regular expression is a pattern that could be matched against an input text. The .Net framework provides a regular expression engine that allows such matching. A pattern consists of one or more character pterals, operators, or constructs.
Constructs for Defining Regular Expressions
There are various categories of characters, operators, and constructs that lets you to define regular expressions. Cpck the following pnks to find these constructs.
The Regex Class
The Regex class is used for representing a regular expression. It has the following commonly used methods −
Sr.No. | Methods & Description |
---|---|
1 | pubpc bool IsMatch(string input) Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string. |
2 | pubpc 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 | pubpc static bool IsMatch(string input, string pattern) Indicates whether the specified regular expression finds a match in the specified input string. |
4 | pubpc MatchCollection Matches(string input) Searches the specified input string for all occurrences of a regular expression. |
5 | pubpc 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 | pubpc string[] Sppt(string input) Sppts an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor. |
For the complete pst of methods and properties, please read the Microsoft documentation on C#.
Example 1
The following example matches words that start with S −
using System; using System.Text.RegularExpressions; namespace RegExApppcation { 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, @"SS*"); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result −
Matching words that start with S : The Expression: SS* Splendid Suns
Example 2
The following example matches words that start with m and ends with e −
using System; using System.Text.RegularExpressions; namespace RegExApppcation { 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, @"mS*e"); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result −
Matching words start with m and ends with e : The Expression: mS*e make maze manage measure
Example 3
This example replaces extra white space −
using System; using System.Text.RegularExpressions; namespace RegExApppcation { 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(); } } }
When the above code is compiled and executed, it produces the following result −
Original String: Hello World Replacement String: Hello WorldAdvertisements