Thinking about Regular Expressions and Console.Writeline

Thinking about Regular Expressions and Console.Writeline

  • Comments 2

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.

Leave a Comment
  • Please add 2 and 4 and type the answer here:
  • Post
  • 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...

  • 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.

Page 1 of 1 (2 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them