Validate address

   

using System;
using System.Text.RegularExpressions;

class RegexSubstitution
{
   public static void Main()
   {

      string text = "124 street";
      if ( !Regex.Match( text, @"^[0-9]+s+([a-zA-Z]+|[a-zA-Z]+s[a-zA-Z]+)$" ).Success ) {
         Console.WriteLine( "Invalid address");
      } 
   }
}


           
          


Use regular to split string


using System;
using System.Text.RegularExpressions;

class RegexSubstitution
{
   public static void Main()
   {
      string testString1 = "1, 2, 3, 4, 5, 6, 7, 8";
      Regex testRegex1 = new Regex( @"d" );

      Console.WriteLine( "Original string: " + testString1 );

      String[] result = Regex.Split( testString1, @",s" );

      foreach ( string resultString in result )
         Console.WriteLine(""" + resultString + "", ");

   }
}

Use regular to replace word


   


using System;
using System.Text.RegularExpressions;

class RegexSubstitution
{
   public static void Main()
   {
      string testString1 = "a b c stars *****";
      Regex testRegex1 = new Regex( @"d" );

      Console.WriteLine( "Original string: " + testString1 );
      Console.WriteLine( "Every word replaced by "word": " + Regex.Replace( testString1, @"w+", "word" ) );
      
   }
}

           
          


Use regular to replace strings


   


using System;
using System.Text.RegularExpressions;

class RegexSubstitution
{
   public static void Main()
   {
      string testString1 = " stars *****";
      Regex testRegex1 = new Regex( @"d" );

      Console.WriteLine( "Original string: " + testString1 );
      testString1 = Regex.Replace( testString1, "stars", "carets" );
      Console.WriteLine( ""carets" substituted for "stars": " + testString1 );
      
   }
}