Thinking about Regular Expressions and Console.Writeline

Are you regular? 

Do you wonder about regular expressions? Let’s take a quick look at a simple use of regular expressions. Alfred Thompson got me thinking about making a pong game, and now I want to do a pong game in a bunch of different ways.

Open a new project in C# and you can cut and past the following into your application, the namespace in this example doesn’t make any difference.

When you run the code you will get a nice output with a header and a simple output.

Code Snippet

  1. using System;
  2.  
  3. namespace DemoImmediateWindows
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int x;
  10.             Console.WriteLine("indexer \t     indexer*2");
  11.             for (int i = 1; i < 10; i = i + 3)
  12.             {
  13.                 Console.WriteLine("  " + i + "\t\t\t"+ i*2);  
  14.             }
  15.         }      
  16.  
  17.     }
  18. }

Your output will look like the following (press ctrl+F5)

image

The “\t” represents a “regular” expression, and the \t is a tab or non-printing character. You can find out more about these characters by going to:

https://msdn.microsoft.com/en-us/library/az24scfc.aspx

You will see some interesting “regular expressions”, for instance the \a is the “bell” character. If you use it in the code above using the following, you will hear some soft beeps. In the old days this would fire off a harsh bell on the telex or teletype machine, sometimes if you watch old movies sometimes you will hear this in a busy newsroom or “war room”. 

Code Snippet

  1. using System;
  2.  
  3. namespace ConsoleWriteLineDemo
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int x;
  10.             Console.WriteLine("indexer \t     indexer*2");
  11.             for (int i = 1; i < 10; i = i + 3)
  12.             {
  13.                 Console.WriteLine("  " + i + "\t\t\t\a\a"+ i*2);  
  14.             }
  15.         }      
  16.  
  17.     }
  18. }

So take a look at the regular expressions and have some fun with them.  You should be able to use this blog to get started and to see your output.