Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Regular Expression in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 225
    Comment on it

    A regular expression is a pattern that could be matched against an input text. Regular expression are used to allow inputs from the user and providing validations.

     

    Regex Class is used for implementing regular expressions, see the example below:

    /* 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 matches words that start with 'm' and ends 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 below example replaces extra white 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();
          }
       }
    }
    
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: