Monthly Archives: August 2007

Regex in Visual Studio

If you’re a daily user of Visual Studio like i am, then you’ll appreciate this nice feature I recently actually figured out how to use: backreferences in regular expressions built within search and/or search & replace operations.

Let’s say I have the following code from a VB to C# conversion, that incorrectly uses method parentheses ( ) when it should be using index brackets [ ] .


if ((grid.Items(intCounter) is GridDataItem) &&grid.Items(intCounter).Display)
{
CheckBox chkSelectForAction = (CheckBox)grid.Items(intCounter).FindControl("chkVal");
if (chkSelectForAction.Checked & chkSelectForAction.Visible)
{
ReturnedList.Add(grid.DataKeyValues(grid.Items(intCounter).ItemIndex)("BindID"));
}
}

And now  let’s say this construct of “.Items(val)” is all over the place, and i want to change it to the correct “.Items[val]” construct.  The obvious problem is that for each match of “.Items(val)”  the inner value (val) may be completely different.

Here is where the power of regular expressions and backreferences helps us.

In the Visual Studio “Find & Replace” window, type the following into the “Find What:” box:

Items\({[^\)]+}\)

In the “Replace With: box, type:

Items\[\1\]

Now let’s explain.  In the first “find” regex, we tell it to find the literal “Items” then the character “(”  — using an escape character \( since we don’t mean to open a match expression.  Then we open the backreference block { }.  This tells VS that when a match is found, to store it in the first backreference (which is accessed via \1).  Then we tell it to find 1 or more of any character other than a closing parenthesis “)” — via [^\)] . This is so the regex doesn’t overrun the bounds of our indexer.  Then we close it up with closing the backreference block } and the ending parenthesis literal \).

Now, when each match of “Items(xxx)” is found, it will store xxx into the backreference of \1 (since there will be only one occurance of the “xxx” part in each match.

And in the same execution, it will fire the “Replace with” block, which accesses this backreference, replacing the entire match with Items[xxx].

And that’s it!  You can probably see how powerful this be when you consider global search and replace operations.  The only trick is to try out your new regex on a selection or single page prior to executing it within an entire project or solution (since you can’t “undo” that operation).

Happy regexing!

4 Comments

Filed under .NET, ASP.NET, C#, Regular Expressions

Stock Chart Anomaly?

DJI Linear

This is Friday’s DJIA chart.  I just happened to notice it this weekend as I was poking around the Yahoo Finance area.

Now I’m no financial guru by any means.  I’m no day-trader and I certainly do not claim to be an expert on the machinations of the Wall Street elite.   However, even I look at this chart and wonder how the hell something as volatile and “chaotic” as the minute by minute fluctuations of the world’s largest stock market could produce a near linear growth for nearly 2 hours.

It seems to me that if trading were suspended during that time, one would expect to see a plateau of sorts.   But a perfectly linear progression of millions upon millions of stocks being bought and sold by literally millions upon millions of different people?  Bueller? Bueller? Anyone??

Leave a comment

Filed under General

1 Billion

If you haven’t realized it yet, we are in the beginning stages of a worldwide credit meltdown. Over the coming weeks, months and even years we will all feel the pain that collectively we’re all responsible for in living such overtly spoiled lives.

Using our homes as ATM machines. Buying cars we can’t afford. Buying homes we have no business owning (and nobody should have lent the money for in the first place) . These poor choices are coming back with a vengeance.

Fortunately for me, I live in a region of the country that has low population, the housing market is relatively stable and the growth therein has been relatively moderate. So the crash won’t be as hard for us locally. However, in all of this it’s easy to lose sight of the fact that with no credit, there will be no buyers in the mortgage arena. With no buyers, the inventories that already exist will start to get desperate. Add to that the people who are starting to get a little uneasy about that 5/25 ARM they refinanced under 2 years ago. 3 years doesn’t seem such a long time now, and with the highest home inventories seen since 1988 coupled with fewer and fewer buyers, it’s easy to see how lots of people are going to get screwed royally.

Be that as it may, in reading story after story about mortgage and banking institutions that are “in trouble” (to say the least), it’s interesting to note how often the figures being thrown around are in the B-as-in-BILLIONs. It’s easy to forget how much a billion dollars is for the average Joe, considering I will never in my life come anywhere near to a billion of anything, much less dollars.

So here’s a few mind warping statistics on exactly how much a billion is:

  • Starting the day you turn 30, walk around for 8 hours a day (every single day, no holidays!) handing out $600 bucks every single minute to strangers. Don’t stop until your 40th birthday. You just spent a billion.
  • Get a nice “six figure” job somewhere earning $100,000 a year. Now do that job for 10,000 years. You’ve just earned a billion bucks.
  • Put a quarter every 30 seconds in that mechanical horse outside the grocery store and ride it for 3,800 years. You just spent a billion.

I think you get the point.

Leave a comment

Filed under Housing Crash