Posts RSS Comments RSS 129 Posts and 246 Comments till now

Archive for September, 2007

Another Proof that Google is much ‘Cooler’ than Yahoo.

Search for this at Google:

Invest $1000000 for Movie

And compare the result from Yahoo:

Invest $1000000 for Movie

Surprisingly, my blog is no.2 at Yahoo’s result, but nowhere in Google’s result. I did even try to search within result in Google by adding word ‘Sodeve’, but nada, zilch, nil .. nothing. :D

 

I know.. I know.. I should be grateful to Yahoo for putting me in the 2nd spot .. but for the sake of fairness to the poor soul who is trying to get an investor to invest $1 Million in his/her movie project, please use Google next time :D

 

As for Yahoo, this means that there are a lot of rooms for improvements in its searching algorithms :D (in case the result changed, I put the screenshot HERE)

C# Regex Word Count

I notice from my traffic log that there was someone bumping to this blog searching for ‘C# Regex Word Count’. Since I never create such tutorial, so here you go.

The basic rule of a word would be it doesn’t contain any whitespaces (spaces, tabs, or newlines). So the easiest Regex would be:

 

[^\ ^\t^\n]+

 

Now to implement it using C#, It would be as follows:

      public int WordCount(String input)
      {
         int result = 0;
         try
         {
            Regex rgx = new Regex(@"[^\ ^\t^\n]+");
            MatchCollection mcMatches = rgx.Matches(input);
            result = mcMatches.Count;
         }
         catch (Exception ex)
         {
         }
         return result;
      }

An improvement would be putting the Regex creation out of the function, this way we will not re-created the Regex everytime the function is called.

Cool Improvement on Double-Decker Bus

Cool SBS

I recently took a trip from my office to Singapore’s Ministry of Manpower using Bus no. 2. As you see on the picture, there is a small lcd screen that indicates the number of seats available on the top-deck. It’s quite neat to save yourself from wasting time looking for available seats by peeking at the top-deck.

 

From the design of the lower-deck which provides wide spaces, I think this bus is specially designed to cater those on the wheelchair. Which was later confirmed when I saw the wheelchair sign in entrance door. This type of bus is mostly available in routes that serving City area (Chinatown, Orchard, Raffles City). I haven’t seen one in my ulu place (a.k.a. Jurong West):D

Note: ulu is a singlish adjective, very likely taken from Malay language, to describe a place that is so far away from ‘human civilization’ :D Well actually, any place in Singapore which is far from Orchard/City area usually labelled as Ulu. :D

Regex to match SQL-Style String

This post is copying the style that is used by Stephen Ostermiller in his article HERE.

 

Motivation

I am currently creating a RichTextBox control that will format and highlight the SQL string. I notice that the current regex that I used is not match correctly to a certain combination of SQL-style string.

 

Consider we have the following SQL statement:

Print ‘Testing Regex\’s String Matching’; Print ‘ ‘;

 

First Try

‘.*’

Print ‘Testing Regex\’s String Matching’; Print ‘ ‘;

This is incorrect. The regex should only match those within two single-quotes, not expand to the last single-quote.

 

(After n-Try)

‘.*?[^\\]’

Print ‘Testing Regex\’s String Matching’; Print ‘ ‘;

This should be the correct one.

 

For those who want to practice their Regular Expression skill, try to download THIS software, it’s free and it’s really good for Regex practice.

Credit goes to Buddie for pointing out this PAGE.

UPDATE (22 Sep 2007)

 

As it turns out, I totally forgot that SQL-style string doesn’t use \’ (backslash-quote) but uses ” (quote-quote). So things getting more complicated now.
So we need to change the string into something like this:

Print ‘Testing Regex’’s String Matching’; Print ‘ ‘;

Another Try

‘[a-zA-Z\ ]*(”)*[a-zA-Z\ ]*’

Print ‘Testing Regex’’s String Matching’; Print ‘ ‘;

But if we change the input string into

Print ‘Testing Regex’’s String’’s Matching’; Print ‘ ‘;

It becomes

Print ‘Testing Regex’’s String’’s Matching’; Print ‘ ‘;

Which is wrong.

 

Finally

‘([a-zA-Z\ ]*(”)*)*’

Print ‘Testing Regex’’s String” Matching’; Print ‘ ‘;

This should be the correct one.

Close
E-mail It
Socialized through Gregarious 42