Regular Expression Pattern in C#

This is a C# Program to illustrate regular expression pattern.

Problem Description

This C# Program Illustrates Regular Expression Pattern.

Problem Solution

Here the Regex class is used for representing a regular expression and the words that start with the given letter is displayed.

Program/Source Code

Here is source code of the C# Program to Illustrate Regular Expression Pattern. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate Regular Expression Pattern
 */
using System;
using System.Text.RegularExpressions;
namespace Application
{
    class Program
    {
        private static void showMatch(string text, string expr)
        {
            Console.WriteLine("The Expression : " + expr);
            MatchCollection m = Regex.Matches(text, expr);
            foreach (Match m1 in m)
            {
                Console.WriteLine(m1);
            }
        }
        static void Main(string[] args)
        {
            string str = "Sanfoundry , a high end Technology Training company";
            Console.WriteLine("Matching words that start with 'S': ");
            showMatch(str, @"\bS\S*");
            Console.ReadKey();
        }
    }
}
Program Explanation

This C# program is used to illustrate regular expression pattern. We have defined a sentence in ‘str’ string variable. Then we are calling the showMatch() procedure by passing the str and expr variable values as an argument.

advertisement
advertisement

The showMatch() procedure is used to find the matching words that start with ‘S’. Here the Regex class is used for representing a regular expression and the words that start with the given letter are displayed.

Runtime Test Cases
 
Matching Words that Starts With 'S' : 
The Expression : \bs\s*
Sanfoundry

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.