C# - Regular Expression Example

In this example, we are going to demonstrating an example of regular expression using C# program.
Submitted by Nidhi, on September 11, 2020 [Last updated : March 22, 2023]

Here, we will demonstrate the use of the regular expression in the C# program. We will print words that are started with 'S'.

C# program to demonstrate the example of regular expression

The source code to demonstrate the use of the regular expression is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the regular expression in C# 

using System;
using System.Text.RegularExpressions;

class Program
    static void Main(string[] args)
        string sample_string = "Includehelp Is The Plateform to Learn New Programing Technologies in Simple Way";
        MatchCollection Result;

        Console.WriteLine("Words that start with 'S': ");
        Result = Regex.Matches(sample_string, @"\bS\S*");
        foreach (Match val in Result)


Words that start with 'S':
Press any key to continue . . .


In the above program, we imported "System.Text.RegularExpressions" namespace to use classes related to regular expressions like Regex, Match, and MatchCollection, etc.

Here we created a sample string and then search the words started with 'S' in the string using the Matches() method of Regex class and then print the result using the "foreach" loop on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.