Do you think IIT Guwahati certified course can help you in your career?
No
Regular Expressions
Regex or Regular Expressions in C# helps us describe complex patterns in texts.
Once you have described these patterns you can use them to do searching, replacing, extracting, and modifying text data.
A regular expression is a pattern that can be matched to a piece of text. The .Net framework has a regular expression engine for this purpose. One or more character literals, operators, or structures make up a pattern.
C# has a built-in API for working with regular expressions; it is located in System.Text.RegularExpressions.
A regular expression defines a search pattern for strings. Regex represents an immutable regular expression. It contains methods to match text, replace text, or split text.
Regular Expression Defining Constructs
Character escapes
These are the so-called "special" or "escape" characters. In a regular expression, the backslash character () signifies that the character after it is either a special character or should be understood literally.
Escape character
Description
Pattern
Matches
\b
It matches the backspace in character class, \u0008.
[\b]{3,}
"\b\b\b\b" in "\b\b\b\b"
\a
It matches the bell character, \u0007
\a
"\u0007" in "cautious!" + '\u0007'
\t
It Matches the tab, \u0009.
(\w+)\t
"Name\t", "Addr\t" in "Name\tAddr\t"
\r
It Matches the carriage return, \u000D. (\r is not equivalent to the newline character, \n.)
The selected parameters are applied or disabled within subexpression.
A\d{2}(?i:\w+)\b
"A12xl", "A12XL" in "A12xl A12XL a12xl"
(?= subexpression)
With a breadth of zero, a positive lookahead assertion is made.
\w+(?=\.)
"is", "ran", and "out" in "He is. The dog ran. The sun is out."
(?! subexpression)
Negative lookahead assertion with zero width.
\b(?!un)\w+\b
"sure", "used" in "unsure sure unity used"
(?< =subexpression)
Zero-width positive lookbehind assertion.
(?< =19)\d{2}\b
"99", "50", "05" in "1851 1999 1950 1905 2003"
Quantifier
Quantifier in Regular Expressions in C# is used to find a match such that For a match to happen, quantifiers describe how many instances of the previous element (which can be a character, a group, or a character class) and it must be present in the input string.
Quantifier
Description
Pattern
Matches
*
The previous element is matched zero or more times.
\d*\.\d
".0", "29.9", "329.9"
+
One or more times matches the previous element.
"co+"
"co" in "code", "co" in "coder"
?
The previous element is matched zero or one time.
"co?e"
"coe", "code"
{ n }
Exactly n times matches the previous element.
",\d{3}"
",043" in "1,043.6", ",876", ",543", and ",210" in "9,876,543,210"
{ n ,}
At least n times matches the previous element.
"\d{2,}"
"166", "29", "1930"
{ n , m }
At least n times, but no more than m times, the previous element is matched.
"\d{3,5}"
"166", "17668" "19302" in "193024"
*?
The previous element is matched zero or more times, but as few as possible.
\d*?\.\d
".0", "19.9", "219.9"
+?
One or more times, but as little as feasible, the previous element is matched.
"c+?"
"co" in "code", "co" in "codeninja"
??
The previous element is matched zero or once, but as few times as feasible.
"co??e"
"coe", "code"
{ n }?
Exactly n times It matches the preceding element.
",\d{3}?"
",043" in "1,043.6", ",876", ",543", and ",210" in "9,876,543,210"
{ n ,}?
It Matches the previous element minimum n times, but try to match as few times as possible.
"\d{2,}?"
"166", "29", "1930"
{ n , m }?
Between n and m times, but as few as feasible, the previous element is matched.
"\d{3,5}?"
"166", "17668" "193", "024" in "193024"
Alternation Constructs Regular Expressions
Alternation construct
Description
Pattern
Matches
|
The vertical bar (|) character is used to separate elements.
co(d|e|i)
"code", "code" in "coi "
(?( expression )yes | no )
It matches yes if the expression matches; else, it matches the optional no part. Expression is what a zero-width assertion is.
(?(A)A\d{2}\b|\b\d{3}\b)
"A10", "910" in "A10 C103 910"
(?( name )yes | no )
It returns yes if the provided capture name matches; else, it returns no.
(?< quoted>")?(?(quoted).+?"|\S+\s)
Ninja.jpg, "raju sleeping.jpg" in "Ninja.jpg "raju sleeping.jpg""
Regex Class
The Regex class displays the.NET Framework's regular expression engine. The Regex class parses a vast amount of text in order to identify a certain character pattern. The Regex class can be used for extracting, modifying, replacing, or deleting the text of a substring. The Regex class is stored in System.Text.RegularExpressions.
Regular Expression Example in C#
Code
// Regular Expressions in C#
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp
{
class DummyProgram
{
static void Main(string[] args)
{
Console.WriteLine("First Program of Regular expression");
string email = "apoorv@gmail.com";
var finalResult = Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
Console.Write("Is valid: {0} ", finalResult);
Console.ReadLine();
}
}
}
Output
First Program of Regular expression
Is valid:True
FAQs
What are Static regular expressions? As an alternative to constantly instantiating a regular expression object with the same regular expression, static regular expression techniques are advised. The regular expression engine caches either the operation codes or the compiled Microsoft intermediate language (MSIL) from patterns used in static method calls, unlike regular expression patterns used by regular expression object
Difference between Interpreted and compiled regular expressions? When calling regular expression methods with a given regular expression on a somewhat infrequent basis, we recommend using interpreted regular expressions. When using regular expression methods with a certain regular expression on a regular basis, you should utilize built regular expressions. It's impossible to say when the slower execution speeds of interpreted regular expressions outweigh the benefits of their shorter startup times, or when the slower startup times of compiled regular expressions outweigh the benefits of their quicker execution speeds.
Key Takeaways
In this article, we have extensively discussed the Regular Expressions in c#.
Until then, All the best for your future endeavors, and Keep Coding. "We hope that this blog has helped you enhance your knowledge regarding this problem, and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!”
Live masterclass
Interview-Ready Excel & AI Skills for Microsoft Analyst Roles
by Prerita Agarwal
19 Jun, 2025
01:30 PM
AI PDF Analyzer using FastAPI – Explained by Google SWE3
by Akash Aggarwal
16 Jun, 2025
01:30 PM
Amazon Data Analyst: Advanced Excel & AI Interview Tips
by Megna Roy
17 Jun, 2025
01:30 PM
From Full Stack to AI Stack: What Modern Web Dev Looks Like
by Shantanu Shubham
18 Jun, 2025
01:30 PM
Interview-Ready Excel & AI Skills for Microsoft Analyst Roles
by Prerita Agarwal
19 Jun, 2025
01:30 PM
AI PDF Analyzer using FastAPI – Explained by Google SWE3