Q:

C# program to demonstrate the example of regular expression

0

C# program to demonstrate the example of regular expression

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the example of Nullable ... >>
<< C# program to demonstrate the use of #define prepr...