use alternation to match either 'cat' or 'hat' - CSharp Language Basics

CSharp examples for Language Basics:Regex

Description

use alternation to match either 'cat' or 'hat'

Demo Code



using System;/*  www  . ja  v  a 2 s  . c  o m*/
using System.Text.RegularExpressions;

class MainClass
{
   static void Main( string[] args )
   {
      string testString = "This is a test for regular expressions or regex or regexp";
      Console.WriteLine( "The test string is\n   \"{0}\"", testString );


      // use alternation to match either 'cat' or 'hat'
      Regex expression = new Regex( "(c|h)at" );
      Console.WriteLine(
         "\n\"hat cat\" matches {0}, but \"cat hat\" matches {1}",
         expression.Match( "hat cat" ), expression.Match( "cat hat" ) );
   }
}

Result


Related Tutorials