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:

http://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.

Comments (2)

  1. Abc says:

    Hmm… t and a are not "regular expressions". Those "things" represents just some characters, that are "presented" by console as beep or tab or something…

  2. SoCal Sam says:

    Abc, you are correct!  Thanks for pointing that out, a regular expression is a pattern that the regular expression engine attempts to match in input text.  The t and a are indeed just characters that are presented by the console.

    In fact these are explained in the following, and are actually more correctly referred to as special characters, and I will need to write a blog to comment on your comment, so thank you for the comment! :)

    Anyway here is a quote from:

    msdn.microsoft.com/…/4edbef7e.aspx

    The backslash () in a regular expression indicates one of the following:

    The character that follows it is a special character, as shown in the table in the following section. For example, b is an anchor that indicates that a regular expression match should begin on a word boundary, t represents a tab, and x020 represents a space.

    A character that otherwise would be interpreted as an unescaped language construct should be interpreted literally. For example, a brace ({) begins the definition of a quantifier, but a backslash followed by a brace ({) indicates that the regular expression engine should match the brace. Similarly, a single backslash marks the beginning of an escaped language construct, but two backslashes (\) indicate that the regular expression engine should match the backslash.