Visual Studio.NET – Search and Replace using Regular Expressions

I’ve always been a fan of regular expressions. They take a while to get your head round, but once you’ve got them figured out, you’ve got a powerful programming ally. In VS.NET you have the ability to use regular expressions within the search and replace feature. The only problem is that whenever I’ve tried it, it doesn’t work as I’d expect to. Weirdly, it doesn’t use the same syntax as the .NET Framework’s Regular Expression library does. Why? Well to be honest I have absolutely no idea!

This morning I started to learn how to use the Search and Replace style regular expressions in Visual Studio.NET. It wasn’t easy, because Microsoft don’t really give you any examples that are useful. They just give you the operator list.

My problem is that I have a horrendous piece of code written by a third party who shall remain anonymous. Let’s just say that it is commercial e-commerce, shopping basket type software written purely in ASP, with no stored procedures and no modular code. I’m also guessing that at least 400 programmers have written add on code over the years as part of “feature requests”, to the point that it now is possibly the worst piece of programming I have ever seen and deserves to be submitted to The Daily WTF Forum. It has one ASP script with over 6000 lines of mixed spaghetti code and HTML. I rest my case.

One thing I hate about ASP programmers is when they forget about the people who might have to read and understand their code at a later date. That is to say; no comments, stupid and inconsistent variable names, undeclared variables, a reliance on “On Error Goto Next” (this should have been renamed to “On Error Ignore”) and bad readability, normally through not using use camel case style mark-up. Hence, I prefer “Request.Form” rather than the harder to read “request.form”. I also find it hard to read when programmers neglect to add some spacing between variable assignments, hence I prefer:

myVar = Request.Form(“foo”)
to
myvar=request.form(“foo”)

It may be a bit picky, but it is simply easier to read for me. I also like some consistency too. So to see “request.Form” on one line and then “request.form” on another just annoys me. VS.NET gives you the ability to use regular expressions to do some of these alterations. The example above with the equals sign is a good one to demonstrate the use of these:

Search for:

{[^:Zs]}={[^:Zs]}
Replace with:
\1 = \2

The curly braces are like groupings in regular .NET expressions. Instead of retrieving these using $1, and $2, you need this \1 and \2. Again, I have no idea why they (being the Visual Studio development team) decided on this format.

This example basically says, search for anything but a space, followed by an equals sign, followed by anything but a space. I did it this way (I know it seems reversed) because there doesn’t seem to be a syntax for a non space character (\S equivalent). The documentation talks about “:Mn”, matching “non-spacing marks”, but I couldn’t get that to work.

So in conclusion, regular expressions can be used effectively in Visual Studio.NET but unfortunately you’ll need to learn an alternative regular expression syntax.

Update: A useful Visual Studio regular expression to remove empty line breaks is “^$\n”.

A good reason not to buy Epson printers

I own a rather new and (actually) rather good Epson printer. Epson,¬†and many other printer manufacturers, have been accused of charging stupidly high prices for ink cartridges, with suggestions that printer ink is more expensive than good champagne, vintage whiskey or Chanel No.5, at a whopping $2,145 per litre. PCMag are reporting that Epson has won a case in Germany, using its patents as a basis to force 4 major retailers to stop selling off-brand ink cartridges. It looks like it is time for a switch to another printer. That, or just buy your off-brand cartridges in a different country via that thing called the “internet”. This news strikes shortly after Epson were caught “lying” and taken to court, because their printer software told customers that the cartridges were empty, when actually they weren’t.

These big manufacturers still haven’t quite got the grasp of how the internet works. Customers can now talk together and discuss the product quality, plus any restrictive practices of manufacturers in the global market place. When a manufacturer screws with the consumer, it isn’t long before they get the unionised internet consumer backlash. Epson should know better, since the majority of their customers peruse the web for peer reviews before they buy. The only people that still go into retail shops to buy printers are our parents, who wouldn’t buy off-brand cartridges anyway. They have traditionally preferred to buy from a person, but even that is slowly changing, with the older generation getting more comfortable with online purchases.

One of these days, a single printer manufacturer is going to figure “it” out, and charge a sensible price for ink cartridges. When I say sensible I mean economically sane – since the off-brand market can only exist because the cost of manufacturing cartridges is way way less than the manufacturers currently charge. Hence any market with massive profit margins can and will be marginalised – natural economics in a competitive market. As a result of this action by a single manufacturer (with a good printer product), the off-brand market will be squeezed out and that first mover will clean up the printer market, shutting out the rest.

The question is, which one has the brains and the balls to do it? Answers on photo paper please!