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”)
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:
\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.