C# program to demonstrate the example of regular expression

Here, we are going to learn about the regular expression and its C# implementation.
Submitted by Nidhi, on September 11, 2020

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

Program:

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)
        {
            Console.WriteLine(val);
        }
    }
}

Output:

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

Explanation:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.