2005-04-25

Simple Perl and C# Regex Comparison

I have been working through Regular Expressions from Friedl. I have been testing the regexes in PowerGrep, EditPad Pro, Perl, and C# in .NET. Here is a very simply comparison:

Here is the Perl script borrowed from Friedl:

print "Enter a temperature (e.g., 32F or 100C):\n";
$input = ;
chomp($input);

if ($input =~ m/^([-+]?[0-9]+(\.[0-9]*)?)([cC][fF])$/)
{
$InputNum = $1;
$type = $3;

if($type eq "C")
{
$celsius = $InputNum;
$fahrenheit = ($celsius * 9 / 5) + 32;
}
else
{
$fahrenheit = $InputNum;
$celsius = ($fahrenheit - 32) * 5 / 9;
}

printf "%.2f C is %.2f F\n", $celsius, $fahrenheit;

}
else
{
print "Expecting a number , then a \"C\" or a \"F\", so don't understand \"$input\".\n";
}

Here is a similar program written in C#:

using System;
using System.Text.RegularExpressions;
namespace Temperature1
{
///


/// Summary description for Class1.
///

class Class1
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter a temperature to convert. (Examples: 0C or 32F)");
string input = Console.ReadLine(); // read input from user
string pattern = @"^([-+]?[0-9]+(?:\.[0-9]*)?)([CF])$"; // build regex pattern
Regex r = new Regex(pattern, RegexOptions.IgnoreCase); // Compile the regular expression.
Match m = r.Match(input); // Match the regular expression pattern against a text string.
if(m.Success)
{
string type = m.Groups[2].ToString();
double celsius = 0.0D;
double fahrenheit = 0.0D;

if(type.ToUpper()=="C") // convert from celsius to fahrenheit
{
celsius = double.Parse(m.Groups[1].ToString());
fahrenheit = (celsius * 9 / 5) + 32;
}
else // convert from fahrenheit to celsius
{
fahrenheit = double.Parse(m.Groups[1].ToString());
celsius = (fahrenheit - 32) * 5 / 9;
}

// at this point we have both temperatures, so display the results
Console.WriteLine("Result: {0,2:n} C is {1,2:n} F", celsius, fahrenheit);
}
else
{
// the initial regex did not match, so issue a warning
Console.WriteLine("Expecting a number followed by 'C' or 'F' so I don't understand {0}.", input);
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
}

Regex in Perl definitely seems more natural and "built in".

Here is a link to Regex documantation on MSDN: .NET Framework Regular Expressions

More to come...

2005-04-21

Working with Perl

I just installed ActivePerl from ActiveState. The installation was painless and so far it has worked perfectly. It's not that I really wanted to learn Perl, it is just that many of the examples in "Regular Expressions, Powerful Techniques for Perl and Other Tools" by Jeffrey E. F. Friedl, from O'Reilly are demonstrated in Perl. It seems like a really concise and powerful language.

Here is my progress...not much I know. Guess this is equivalent to "Hello World" for demonstrating regular expressions in Perl.

print "Enter a temperature in Celsius:\n";
$celsius = ;
chomp($celsius);

if ($celsius =~ m/^([-+]?[0-9]+)([CF])$/) {
$fahrenheit = ($celsius * 9 / 5) + 32;
printf "%.2f C is %.2f F\n", $celsius, $fahrenheit;
}
else{
print "Expecting a number, so don't understand \"$celsius\".\n";
}

2005-04-18

Check out SpamGourmet

Eugeny Sattler informed me of a great Spam protection service. It is called www.spamgourmet.com. He did russian translation for the site www.spamgourmet.com It is a great site for spam protection, and he recommends it to everybody.

How spamgourmet works

If you give your email address to everyone, you are bound to receive spam emails, and you won't know where they came from. Wouldn't it be convenient to give a different email address to every business or web site, while getting all your email as before? Wouldn't it be easiest to assume the address will be given to spammers, and have it work as a spam blocker by shutting off automatically unless you decide otherwise?

That's exactly what spamgourmet offers!...

2005-04-17

Arkansas River Beatis Hatch

Dave and I fished the Arkansas River upriver from Buena Vista, CO. Dick from ArkAnglers sent us there. It was a gorgeous spot and a beautiful day. There was too much sun (I know, what a terrible problem to have ;-) for much surface action early in the day. The clouds moved in and I'm sure the baetis hatch started right after we had to leave, but that is how it goes.

We were catching mostly browns and a few rainbows. Early in the day we were producing on green copper johns, Barr's emergers and olive foamwinged flashback RSII's. It got pretty warm and I had a slow spell for a good hour or two. Dave kept nailing them in the last whole with a barr's emerger. He was "working it". Slow, short, steady drift with lots of success. It was really cool to watch.

Here is a current fishing report from Arkanglers

2005-04-14

Regular Expressions

I was just tasked with an interesting programming challenge this week. I used Regular expressions to solve it. I'm a total novice at using regular expressions, but I do see the value in using them for certain tasks. Wow, what a powerful technology!

I posted a question on a Google Group: Regex. I very generous and helpful developer by the name of Eugeny Sattler assisted me in solving this problem and helped me to gain a better understanding of Regular Expressions in the process.

I am evaluating a tools that take advantage of Regular Expressions. One being EditPad Pro. This is an excellent tool and I'm seriously considering purchasing this tool. I have been using EditPad Lite for a while now and really like some of it's features. The Pro version is just that much more powerful. Another tool that might be worth purchasing is RegexBuddy.

I am reading "Regular Expressions, Powerful Techniques for Perl and Other Tools" by Jeffrey E. F. Friedl, from O'Reilly. This is an excellent book. It is written in a way that really aids in the understanding of an otherwise complicated topic. I have only gotten through the first chapter so far, but I am already getting some of the basics down.

Another tool you might consider adding to your aresenal is a pre-compiled, windows-native version of GNU egrep can be found in the bundle at: http://unxutils.sourceforge.net/. Lots of goodies in there.

2005-04-09

Great Birthday!

Had a great birthday. Dave picked me up at ~5:30 AM, we drove to the Colorado near Parshall, CO, we fished for a few hours and headed back early that afternoon. Dave is a hell of a guy to get up that early. He wasn't even grumpy.

The water was up from last week and a bit off-color. I was thinking that we weren't going to do very well but Dave picked up a few early on. We seined the river and found what looked like large dark sow bugs. I didn't know that there were sow bugs on the Colorado. Let me know if you know otherwise. We also found a ton of small baetis and other stuff. We put on a Ray Charles and an olive foam wing, flashback RSII and spanked them. Together Dave and I probably landed 40 fish. Great day on the water.

That evening, I went to the Galactic concert at the Fillmore Auditorium with Caroline, Kati, and my sister Nicole. Good show but I miss what Theryl added to Galactic. Maceo Parker was the opening band.


Gorgeous day on the Colorado


Diamond Dave working the seam


Gurkha Cigar


Dave with a nice brown


Releasing it for another day Posted by Hello

2005-04-02

Willams Fork Confluence

Had a great fishing below the Williams Fork and Colorado Confluence. Got there at 7:15 AM and was the second guy in the lot. By the time I had my waders and vest on, there about 5 more cars all jockeying for position. I ended up with a good spot and caught a fair amount of fish on a glass bead, black D-rib midge. The fish started rising so moved the D-rib to the bottom and put an emerging Black Beauty on (Black Beauty with a z-lon emerging wing case). Both flies continued to catch fish.

I Met a nice guy Paul and learned a lot from him. I was fishing below another gentleman and he was doing really well. He had the best spot and got em on emergers and dries. I couldn't reach the risers and tried a few different dry\dropper combos with no luck.

I had to leave the fish at 11:00 to meet my wifey and kiddo at Mary Jane. We took turns hanging with Maddie and had great spring skiing. Sweet day all around!