GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — C# - Regular Expressions

Csharp Tutorial
  • C# - Home
  • C# - Overview
  • C# - Environment
  • C# - Program Structure
  • C# - Basic Syntax
  • C# - Data Types
  • C# - Type Conversion
  • C# - Variables
  • C# - Constants
  • C# - Operators
  • C# - Arithmetic Operators
  • C# - Assignment Operators
  • C# - Relational Operators
  • C# - Logical Operators
  • C# - Bitwise Operators
  • C# - Miscellaneous Operators
  • C# - Operators Precedence
  • C# Conditional Statements
  • C# - Decision Making
  • C# - If
  • C# - If Else
  • C# - Nested If
  • C# - Switch
  • C# - Nested Switch
  • C# - Switch Expressions
  • C# Control Statements
  • C# - Loops
  • C# - For Loop
  • C# - While Loop
  • C# - Do While Loop
  • C# - Nested Loops
  • C# - Break
  • C# - Continue
  • C# - Foreach Loop
  • C# - Goto Statement
  • C# OOP & Data Handling
  • C# - Encapsulation
  • C# - Methods
  • C# - Nullables
  • C# - Arrays
  • C# - Strings
  • C# - Structure
  • C# - Enums
  • C# - Classes
  • C# - Inheritance
  • C# - Polymorphism
  • C# - Operator Overloading
  • C# - Interfaces
  • C# - Namespaces
  • C# - Preprocessor Directives
  • C# - Regular Expressions
  • C# - Custom Exceptions
  • C# - Exception Handling
  • C# - File I/O
  • C# Advanced Tutorial
  • C# - Attributes
  • C# - Reflection
  • C# - Properties
  • C# - Indexers
  • C# - Delegates
  • C# - Events
  • C# - Collections
  • C# - Generics
  • C# - LINQ
  • C# - IEnumerable vs IEnumerator
  • C# - Anonymous Methods
  • C# - Unsafe Codes
  • C# - Tasks and Parallel Programming
  • C# - Multithreading
  • C# - Extension Methods
  • C# - Lambda Expressions
  • C# - Async and Await
  • C# Modern Features
  • C# - Tuples
  • C# - Records
  • C# - Pattern Matching Enhancements
  • C# - Top-level Statements
  • C# - Nullable Reference Types
  • C# - What's New in C# 11 / 12 / 13
  • C# - Global Usings
  • C# - File-Scoped Namespaces
  • C# Practical & Advanced Usage
  • C# - JSON & XML Handling
  • C# - Data Serialization & Deserialization
  • C# - REST API Calls with Httpclient
  • C# - Dependency Injection
  • C# - Unit Testing with NUnit, xUnit & MSTest
  • C# - Package Management with NuGet
  • C# Useful Resources
  • C# - Developer's AI Tools
  • C# - Questions and Answers
  • C# - Cheatsheet
  • C# - Quick Guide
  • C# - Useful Resources
  • C# - Discussion
  • C# - Online Compiler
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C# - Regular Expressions



Previous
Quiz
Next

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 literals, operators, or constructs.

Constructs for Defining Regular Expressions

There are various categories of characters, operators, and constructs that lets you to define regular expressions. Click the following links to find these constructs.

  • Character escapes

  • Character classes

  • Anchors

  • Grouping constructs

  • Quantifiers

  • Backreference constructs

  • Alternation constructs

  • Substitutions

  • Miscellaneous 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

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.

For the complete list of methods and properties, please read the Microsoft documentation on C#.

Use of Regular Expressions

The following are some uses of regular expressions:

  • Pattern Matching – Regular expressions are used to find text patterns in strings.
  • Validation – Regular expressions are used for different types of validations such as validating email, phone numbers, etc.
  • Replacement – Regular expressions can also be used to replace specific patterns in text.
  • Extraction – You can also use regular expressions to extract specific parts of a string based on your requirements.

Required Namespaces for Regular Expressions (RegEx)

To work with Regex in C#, you need to include:

using System;
using System.Text.RegularExpressions;

Basic Regex Syntax

Here are the syntaxes of some essential Regex elements:

Pattern Description Example
. Any single character "h.t" matches "hat", "hit"
\d Any digit (0-9) "abc123" = Matches 123
\w Any word character (a-z, A-Z, 0-9, _) "hello_world"
\s Any whitespace "hello world"
\b Word boundary "\bcat\b" matches "cat", not "category"
^ Start of string "^Hello" matches "Hello" at start
$ End of string "World$" matches "World" at end
* Zero or more occurrences "A*" matches "A", "AA", ""
+ One or more occurrences "A+" matches "A", "AA", not ""
{n,m} Between n and m occurrences "\d{2,4}" matches 12, 123, 1234

String Pattern Matching Using RegEx

You can check whether a string matches a specific pattern by using the Regex.Matches() function.

Example

The following example matches words that start with 'S' −

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 = "A Thousand Splendid Suns";
         
         Console.WriteLine("Matching words that start with 'S': ");
         showMatch(str, @"\bS\S*");
         Console.ReadKey();
      }
   }
}

The following example demonstrates matching words that start with 'S' in the string −

Matching words that start with 'S':
The Expression: \bS\S*
Splendid
Suns

Example

The following example matches words that start with 'm' and end with 'e' −

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();
      }
   }
}

The following example demonstrates matching words that start with 'm' and end with 'e' −

Matching words start with 'm' and ends with 'e':
The Expression: \bm\S*e\b
make
maze
manage
measure

String Replacement Using RegEx

You can replace multiple spaces with a single space in a string using the Regex.Replace() function.

Example

The following example replaces extra white spaces in a string with a single space −

using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
   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();
      }
   }
}

The following example demonstrates replacing multiple spaces with a single space −

Original String: Hello   World   
Replacement String: Hello World   

Email Validating Using RegEx

You can validate an email address using a regular expression that checks for proper format (e.g., [email protected]).

Example

The following example demonstrates how to validate an email address using regex −

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      static void Main(string[] args) {
         string email = "[email protected]";
         string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";

         Regex rgx = new Regex(pattern);
         bool isValid = rgx.IsMatch(email);

         if (isValid) {
            Console.WriteLine("The email address is valid.");
         } else {
            Console.WriteLine("The email address is invalid.");
         }

         Console.ReadKey();
      }
   }
}

The following example demonstrates how to validate an email address using regex −

The email address is valid.
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.