Match regex or regexp using an optional 'p' - CSharp Language Basics

CSharp examples for Language Basics:Regex

Description

Match regex or regexp using an optional 'p'

Demo Code



using System;//  w  w w  .j a  v  a 2 s .com
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 );
 
      Console.Write(
         "\nMatch \"regex\" or \"regexp\" using an optional 'p': " );

      // use the ? quantifier to include an optional 'p'
      foreach ( var myMatch in Regex.Matches( testString, "regexp?" ) )
         Console.Write( "{0} ", myMatch );      
   }
}

Result


Related Tutorials