Table of contents
1.
Regular Expressions
1.1.
Regular Expression Defining Constructs
1.2.
Character escapes
2.
Character Classes
3.
Anchors
4.
Grouping Constructs
5.
Quantifier
6.
Alternation Constructs Regular Expressions
7.
Regex Class
7.1.
Regular Expression Example in C#
7.1.1.
Code
7.1.2.
Output 
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Regular Expressions in C#

Author Apoorv
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.) \r\n(\w+) "\r\nHello" in "\r\Hello\nNinja."
\v This character Matches the vertical tab, \u000B. [\v]{2,} "\v\v\v" in "\v\v\v"
\f This character Matches the form feed, \u000C. [\f]{2,} "\f\f\f" in "\f\f\f"
\n This character Matches the new line, \u000A. \r\n(\w+) "\r\nHello" in "\r\Hello\nNinja."

Recommended topics, Palindrome in C# and Ienumerable vs Iqueryable

 

Character Classes

A character class is a set of characters that matches any one of them.

Character class Description Pattern Matches
[character_group] This Matches any single character in character_group. The match is case sensitive by default. [cn] "c" in "code" "c", "n" in "codeninja"
[^character_group] Negation: Any single character that isn't in the character group is matched. Characters in this character group are by default case-sensitive. [^aei] "c", "o" in "code"
[ first - last ] Character range: Matches any single character from the first to the last in the range. [b-d] [b-d]oy Boy Coy Doy
\p{ name } Except for n, a wildcard matches any single character. \p{Lu} "C", "L" in "City Lights"
\P{ name } Any character in the Unicode general category or named block supplied by name is matched. \P{Lu} "i", "t", "y" in "City"
. \u000D corresponds to a carriage return. (The newline character, \n, is not equal to the character \r.) a.e "ave" in "have" "ate" in "mate"
\w Matches any word character \w "C", "O", "D" and "1" in "Code#1"
\W Matches any non-word character. \W "#" in "Code#1"
\s Matches any white-space character. \w\s "D " in "ID A1.3"
\S Matches any non-white-space character. \s\S " _" in "int __ctr"

Anchors

Anchors determine whether a match succeeds or fails based on the string's present position.

Assertion

Description

Pattern

Matches

^ The match must begin with the first character in the string or line. ^\d{3} "789" in "789-999-"
$ The match must occur before \n at the end of the line or string, or at the end of the string. -\d{4}$ "-2000" in "8-12-2000"
\A The match must take place at the beginning of the string. \A\w{3} "Ninja" in "Ninja-008-"
\Z The match must occur either at the end of the string or before the \n at the end. -\d{3}\Z "-008" in "Bond-901-008"
\z The match must take place at the very end of the string. -\d{3}\z "-444" in "-901-444"
\G The next match must begin where the last match concluded. \\G\(\d\) "(1)", "(2)", "(3)" in "(1)(2)(3)[4](6)"
\b A border between a \w (alphanumeric) and a \W(nonalphanumeric) character must contain the match. \w "C", "0", "D" and "1" in "Code#1"
\B The match can't happen on a \b boundary. \Bend\w*\b "ends", "ender" in "end sends endure lender"

Grouping Constructs

Grouping structures separate regular expression subexpressions and capture substrings from an input string.

Grouping construct

Description

Pattern

Matches

( subexpression )

Captures the matched subexpression and assigns it a zero-based ordinal number.

(\w)\1

"ss" in "mass"

(?< name >subexpression)

Captures the matched subexpression into a named group.

(?< double>\w)\k< double>

"ss" in "mass"

(?< name1 -name2 >subexpression)

Defines a balancing group definition.

(((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*(?(Open)(?!))$

"((1-3)*(3-1))" in "3+2^((1-3)*(3-1))"

(?: subexpression)

Defines a noncapturing group.

Write(?:Line)?

"WriteLine" in "Console.WriteLine()"

(?imnsx-imnsx:subexpression)

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

  1. 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
  2. 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