- HTML Comments Tutorial | Convert Comments into HTML Codes
- Data Structure and Algorithms Tutorial | Ultimate Guide to Learn
- Gradle Tutorial | For Beginners [ STEP-IN ]
- Encapsulation In Java | Complete Tutorial With Examples
- What is Release Management? | A Comprehensive Tutorial for Beginners
- OOPs Concepts in Java | Learn from Basics with Examples
- The Top Basic Tools of Quality Tutorial | The Ultimate Guide
- Set Environment for C# – Learn How to Setup through this Tutorial
- C# Vs Java Tutorial | Overview and Key Difference
- List of IDEs to run C# Programs | Tutorial for Learning Path
- C Sharp Variables and Constants | The Ultimate Guide
- Unsafe Code in C-Sharp Tutorial | Everything You Need to Know
- Type Conversion Method in C# | A Complete Tutorial
- What Is Synchronization in c# Tutorial | The BEST Step-By-Step Guide
- Understanding Structures in C# | Learn in 1 Day FREE Tutorial
- Strings – C# Tutorial | A Definitive Programming Guide
- Static Keyword in C# Tutorial | Learn with Examples
- Stack Collection in C# Tutorial | A Definitive Guide for Beginners
- C# Sorted List Tutorial with Examples | Learn in 1 Day FREE
- C# Serialization Tutorial | Ultimate Guide to Learn [BEST & NEW]
- Regular Expression in C# Tutorial | Everything You Need to Know
- What is Reflection in C#? | Learn Now Tutorial
- Queue Collection in C# Tutorial | A Definitive Guide
- Properties in C# | The complete Tutorial
- C# Preprocessor Directives Tutorial | Learn in 1 Day FREE
- Polymorphism C# Tutorial | The Ultimate Guide
- C# Operators Tutorial | Learn Arithmetic, Comparison, Logical Concepts
- Namespaces – The complete C# Tutorial
- Multithreading in C# Tutorial | Learn With Examples and Advantages
- Methods – C# Tutorial | A Complete Programming Guide
- Linked List Implementation in C# Tutorial | Ultimate Guide to Learn [UPDATED]
- Introduction to C# Tutorial | Guide for Beginners
- What is Interface in C# | A Defined Free Tutorial
- C# Inheritance Tutorial | A Complete Free Tutorial
- Indexers in C# Tutorial | A Complete Programming Guide
- HashSet Collection in C# Tutorial | Complete Guide Tutorial For Free
- Generics in C# Tutorial | Learn Generic Classes and Methods
- Creating Your First C# Program Tutorial | Learn in 1 Day
- Basics of File Handling in C# Tutorial | The Ultimate Guide
- C# Exception Handling Tutorial | Learn with Best Practices
- Events – C# Tutorial | A Complete Programming Guide
- C# Enumerations Type Tutorial | Learn Everything about Enum
- Dictionary Collection in C# | Ultimate Guide to Learn [NEW & UPDATED]
- Delegates – C# Programming Guide | The Ultimate Guide for Beginners
- Understanding Decision Making Statements in C# | Learn Now Tutorial
- Classes and Objects – C# Fundamentals Tutorial
- C# BitArray Collection Tutorial | Learn in 1 Day FREE
- Attributes in C# Tutorial | Learn to work with attributes in C#
- C# Array Tutorial | Create, Declare, Initialize
- ArrayList Collection on in C# | A Complete tutorial For Beginners
- Anonymous Methods and Lambdas – C# Tutorial | A Complete Guide
- Abstraction in C# Tutorial – Learn the Abstract class and Interface
- Game Development using Unity 3D Tutorial | Ultimate Guide to Learn [UPDATED]
- C++ Reference Tutorial | A Comprehensive Guide for Beginners
- PHP vs Python | Which Is Better For Web Development
- C++ Constructors Tutorial: Types and Copy Constructors
- JavaScript Arrays Tutorial | Complete Beginner’s Guide
- What Is Maven | Maven Tutorial For Beginners
- Spring Tutorial | Perfect Guide for Beginners
- React Hooks Tutorial for Beginners | Ultimate Guide to Learn
- Python for Data Science Tutorial | Quickstart : A Complete Guide
- What is Golang? : A tutorial for beginners | Get started
- Hibernate Validator Tutorial | Learn in 1 Day FREE
- Postman Tutorial for Beginners: API Testing using Postman | A Complete Guide
- Akka Tutorial
- J2EE | Web Development Tutorial for Beginners
- Scala Exception Handling Tutorial | Learn in 1 Day [ STEP-IN ]
- Web development Tutorial
- Visual Studio Tutorial
- PyGame Tutorial
- Python Anaconda Tutorial
- Python Scikit-Learn Cheat Sheet Tutorial
- Mean Stack Tutorial
- Python Requests Tutorial
- Advanced Java Tutorial
- Spring Boot Microservices Tutorial
- Java Servlets Tutorial
- How to Install Pycharm
- Pycharm Tutorial
- Python Version Tutorial
- Python strings
- How to Download Python
- C Data Types Tutorial
- arrays in python
- Python While Loop Tutorial
- JAVA Tutorial
- Loops In C Tutorial
- Java File I/O Tutorial
- Variables in Python Tutorial
- Python Tutorial
- Python Pandas Cheat Sheet Tutorial
- Data Structures Cheat Sheet with Python Tutorial
- Python Tuples Tutorial
- Python If Else Statements Tutorial
- Python Functions Tutorial
- HTML Comments Tutorial | Convert Comments into HTML Codes
- Data Structure and Algorithms Tutorial | Ultimate Guide to Learn
- Gradle Tutorial | For Beginners [ STEP-IN ]
- Encapsulation In Java | Complete Tutorial With Examples
- What is Release Management? | A Comprehensive Tutorial for Beginners
- OOPs Concepts in Java | Learn from Basics with Examples
- The Top Basic Tools of Quality Tutorial | The Ultimate Guide
- Set Environment for C# – Learn How to Setup through this Tutorial
- C# Vs Java Tutorial | Overview and Key Difference
- List of IDEs to run C# Programs | Tutorial for Learning Path
- C Sharp Variables and Constants | The Ultimate Guide
- Unsafe Code in C-Sharp Tutorial | Everything You Need to Know
- Type Conversion Method in C# | A Complete Tutorial
- What Is Synchronization in c# Tutorial | The BEST Step-By-Step Guide
- Understanding Structures in C# | Learn in 1 Day FREE Tutorial
- Strings – C# Tutorial | A Definitive Programming Guide
- Static Keyword in C# Tutorial | Learn with Examples
- Stack Collection in C# Tutorial | A Definitive Guide for Beginners
- C# Sorted List Tutorial with Examples | Learn in 1 Day FREE
- C# Serialization Tutorial | Ultimate Guide to Learn [BEST & NEW]
- Regular Expression in C# Tutorial | Everything You Need to Know
- What is Reflection in C#? | Learn Now Tutorial
- Queue Collection in C# Tutorial | A Definitive Guide
- Properties in C# | The complete Tutorial
- C# Preprocessor Directives Tutorial | Learn in 1 Day FREE
- Polymorphism C# Tutorial | The Ultimate Guide
- C# Operators Tutorial | Learn Arithmetic, Comparison, Logical Concepts
- Namespaces – The complete C# Tutorial
- Multithreading in C# Tutorial | Learn With Examples and Advantages
- Methods – C# Tutorial | A Complete Programming Guide
- Linked List Implementation in C# Tutorial | Ultimate Guide to Learn [UPDATED]
- Introduction to C# Tutorial | Guide for Beginners
- What is Interface in C# | A Defined Free Tutorial
- C# Inheritance Tutorial | A Complete Free Tutorial
- Indexers in C# Tutorial | A Complete Programming Guide
- HashSet Collection in C# Tutorial | Complete Guide Tutorial For Free
- Generics in C# Tutorial | Learn Generic Classes and Methods
- Creating Your First C# Program Tutorial | Learn in 1 Day
- Basics of File Handling in C# Tutorial | The Ultimate Guide
- C# Exception Handling Tutorial | Learn with Best Practices
- Events – C# Tutorial | A Complete Programming Guide
- C# Enumerations Type Tutorial | Learn Everything about Enum
- Dictionary Collection in C# | Ultimate Guide to Learn [NEW & UPDATED]
- Delegates – C# Programming Guide | The Ultimate Guide for Beginners
- Understanding Decision Making Statements in C# | Learn Now Tutorial
- Classes and Objects – C# Fundamentals Tutorial
- C# BitArray Collection Tutorial | Learn in 1 Day FREE
- Attributes in C# Tutorial | Learn to work with attributes in C#
- C# Array Tutorial | Create, Declare, Initialize
- ArrayList Collection on in C# | A Complete tutorial For Beginners
- Anonymous Methods and Lambdas – C# Tutorial | A Complete Guide
- Abstraction in C# Tutorial – Learn the Abstract class and Interface
- Game Development using Unity 3D Tutorial | Ultimate Guide to Learn [UPDATED]
- C++ Reference Tutorial | A Comprehensive Guide for Beginners
- PHP vs Python | Which Is Better For Web Development
- C++ Constructors Tutorial: Types and Copy Constructors
- JavaScript Arrays Tutorial | Complete Beginner’s Guide
- What Is Maven | Maven Tutorial For Beginners
- Spring Tutorial | Perfect Guide for Beginners
- React Hooks Tutorial for Beginners | Ultimate Guide to Learn
- Python for Data Science Tutorial | Quickstart : A Complete Guide
- What is Golang? : A tutorial for beginners | Get started
- Hibernate Validator Tutorial | Learn in 1 Day FREE
- Postman Tutorial for Beginners: API Testing using Postman | A Complete Guide
- Akka Tutorial
- J2EE | Web Development Tutorial for Beginners
- Scala Exception Handling Tutorial | Learn in 1 Day [ STEP-IN ]
- Web development Tutorial
- Visual Studio Tutorial
- PyGame Tutorial
- Python Anaconda Tutorial
- Python Scikit-Learn Cheat Sheet Tutorial
- Mean Stack Tutorial
- Python Requests Tutorial
- Advanced Java Tutorial
- Spring Boot Microservices Tutorial
- Java Servlets Tutorial
- How to Install Pycharm
- Pycharm Tutorial
- Python Version Tutorial
- Python strings
- How to Download Python
- C Data Types Tutorial
- arrays in python
- Python While Loop Tutorial
- JAVA Tutorial
- Loops In C Tutorial
- Java File I/O Tutorial
- Variables in Python Tutorial
- Python Tutorial
- Python Pandas Cheat Sheet Tutorial
- Data Structures Cheat Sheet with Python Tutorial
- Python Tuples Tutorial
- Python If Else Statements Tutorial
- Python Functions Tutorial
Regular Expression in C# Tutorial | Everything You Need to Know
Last updated on 16th Aug 2022, Blog, Software Engineering, Tutorials
Msdn regex
Regex
The term Regex means for Regular expression. The regex or regexp or regular expression is a sequence of various characters which describe the particular search pattern. It is also called a Rational expression.It is majorly used for searching and manipulating text strings.Can simply search the pattern and replace them with the matching pattern with the help of regular expression.
Regular Expression Characters
There are different type of characters of a regular expression:
- 1. Metacharacters
- 2. Quantifier
- 3. Groups and Ranges
- 4. Escape Characters or character classes
C# Regex class syntax

The basic syntax are used for regular expressions are,
Quantifiers
The very important quantifiers are *?+.
1. * => Match the preceding character zero or more times.
2. + => Match the preceding character 1 or more times.
3. ? => Match the preceding char zero or one time.
Special characters:
More special characters are available for regex building. Here are some of the more usual ones.
1. ^ => It is used to match the starting of a string.
2. $=> It is used to match the end of the string.
3. (Dot) => Matches any character to only once.
4. \d => It is used to match the digit character.
5. \D => It is used to match any non-digit character.
6. \w => It is used to match the alphanumeric character plus “_”.
7. \W => It is used to match any non-word character.
8. \s => Matches the white space characters.
9. \S => Matches the non-white space character.
10. \n =>Matches the newline character.
Constructors in Regex Class

The various constructors and their description is given:
Table: Constructors in Regex Class in C#
Source: MSDN
| Constructors | Description |
|---|---|
| Regex() | This constructor initiates a new instance of the Regex class. |
| Regex(SerializationInfo, StreamingContext) | This constructor initiates a new instance of the Regex class by using serialized data. |
| Regex(String) | This constructor initiate a new instance of the Regex class for the particular regular expression |
| Regex(String, RegexOptions) | This constructor initiates a new instance of the Regex class for the particular regular expression, with options that modify the pattern |
| Regex(String, RegexOptions, TimeSpan) | This constructor initiates a new instance of the Regex class for the particular regular expression, with options that modify the pattern and a value that specifies how long a pattern matching method should attempt the match before it times out. |
Fields in Regex Classz
The various fields and their description is given as follows:
Table: Fields in Regex Class in C#
| Fields | Description |
|---|---|
| Capnames | This field is used by a Regex object created by the CompileToAssembly method. |
| Caps | This field is used by a Regex object created by the CompileToAssembly method. |
| Capsize | This field is used by a Regex object created by the CompileToAssembly method. |
| Capslist | This field is used by a Regex object created by the CompileToAssembly method. |
| Factory | This field is used by a Regex object created by the CompileToAssembly method. |
| InfiniteMatchTimeout | This timeout is used by a Regex object created by the CompileToAssembly method. |
| internalMatchTimeout | This timeout is the maximum amount of time that can elapse in the pattern-matching operation before operation timed out. |
| pattern | This field is used by a Regex object created by the CompileToAssembly method. |
| roptions | This field is used by a Regex object created by the CompileToAssembly method. |
Properties in Regex Class
The various properties and their description is given as follows:
Table: Properties in Regex Class in C#
Source: MSDN
| Properties | Description |
|---|---|
| CacheSize | This property gets or sets the maximum number of entries in a current static cache of compiled regular expressions. |
| CapNames | This property gets or sets a dictionary that maps named the capturing groups to their index values. |
| Caps | This property gets a dictionary that maps numbered capturing groups to the index values. |
| MatchTimeout | This property gets the time-out interval of a current instance. |
| Options | This property gets the options that were passed into a Regex constructor. |
| RightToLeft | This property gets a value that denotes whether the regular expression searches from right to left. |
Methods in Regex Class
The various methods and their description is given:
Table: Methods in Regex Class in C#
Source: MSDN
| Methods | Description |
|---|---|
| CompileToAssembly(RegexCompilationInfo[], AssemblyName) | This method compiles one or more particular Regex objects to a named assembly. |
| Equals(Object) | This method determines whether the particular object is equal to the current object. |
| Escape(String) | This method escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with the escape codes. This instructs a regular expression engine to interpret these characters literally rather than as metacharacters. |
| GetGroupNames() | This method returns an array of capturing the group names for the regular expression. |
| GetGroupNumbers() | This method returns an array of capturing group numbers that correspond to group names in the array. |
| GetHashCode() | This method serves as a default hash function. |
| GetType() | This method gets the Type of a current instance. |
| GroupNameFromNumber(Int32) | This method gets the group name that corresponds to the particular group number. |
| InitializeReferences() | This method is used by a Regex object created by the CompileToAssembly method. | .
| IsMatch(String) | This method denotes whether the regular expression particular in the Regex constructor finds a match in a specified input string. |
| IsMatch(String, String, RegexOptions) | This method denotes whether the specified regular expression finds a match in the specified input string, using the specified matching options. |
| Match(String) | This method finds the specified input string for the first occurrence of the regular expression specified in the Regex constructor. |
| Match(String, Int32) | This method searches the input string for the first occurrence of a regular expression, beginning at the specified starting position in a string. |
| Matches(String) | This method finds the specified input string for all occurrences of the regular expression. |
| Matches(String, Int32) | This method finds the particular input string for all occurrences of a regular expression, beginning at the specified starting position in a string. |
| MemberwiseClone() | This method generates a shallow copy of the current Object. |
| Replace(String, MatchEvaluator) | This method replaces all the strings that match a specified regular expression with a string returned by a MatchEvaluator delegate. |
| Replace(String, MatchEvaluator, Int32) | This method replaces a specified maximum number of strings that match a regular expression pattern with the string returned by the MatchEvaluatordelegate. |
| Split(String) | This method splits an input string into an array of substrings at the positions explained by a regular expression pattern specified in the Regex constructor. |
| Split(String, Int32) | This method splits an input string a specified maximum number of times into an array of substrings, at the positions explained by a regular expression specified in the Regex constructor. |
| ToString() | This method returns the regular expression pattern that was passed into a Regexconstructor. |
| Unescape(String) | This method converts any escaped characters in an input string. |
| UseOptionC() | This method is used by a Regex object created by the CompileToAssembly method. |
| UseOptionR() | This method is used by a Regex object created by the CompileToAssembly method. |
| ValidateMatchTimeout(TimeSpan) | This method checks a time-out interval is within an acceptable range. |