Optional, Single-Instance Identifier in VS Regex Search

By Michael Flanakin @ 11:40 AM :: 210 Views :: 1 Comments :: Visual Studio, (Open), (Reported) :: Digg it!

Visual Studio's regular expression (regex) search isn't quite standard, as I've mentioned before. Fortunately, you can do most of what you need despite this. One thing missing, however, is the optional character/word identifier (question mark in "standard" regex). For instance, colou?r should return color and colour. The only work-around I know of is to use the 0-to-many occurrences identifier (asterisk). This means you'd search for colou*r, which would give you color and colour; however, it would also give you colouuuuuuuuuuuuuuur. As you can see, this would be a problem.

Luckily, in most scenarios, the lack of the optional identifier won't affect you too much. This is assuming you're searching compilation-ready code, that is. Let's use a VB search as an example. Searching for :i\.ToString(\(\))* should return something.ToString() and something.ToString().

Like I said, this scenario works just fine because, if your code is compilable, you should never have something.ToString()(). There are, however, circumstances where the 0-to-many identifier won't suffice. Of course, these are harder to come up with since they're more rare, but I'll do my best... Let's consider a search for declared strings and one-dimension string arrays, string(\[\])*:Wh+:i, which is intended to find string abc and string[] xyz. As you might imagine, there is a likelihood that you may also get string[][] abc, which is the problem in this case. Granted, I realize this may not be an ideal example, but the problem usually only comes into play when you have long, complicated search strings. The work-around for this case is to use the or operator: (string|string\[\]):Wh+:i. This will return either string abc or string[] xyz, but nothing else. As you can see, this makes the search string longer. Now, imagine if you're doing an even more complicated search for string-checking (i.e. someString.Equals(""), someString.Equals(anotherString), !someString.Equals(""), someString == "", someString != "", someString == null, someString != null, someString == null || someString.Length == 0, or someString != null && someString.Length > 0... just to name a few). The possibilities are seemingly endless. Of course, this is going to be a ridiculously long search string, anyway, but the optional identifier would make this so much easier.

FYI: :i is a VS-only shortcut that represents a C++ identifier ([a-zA-Z_$][a-zA-Z0-9_$]*)

FYI: :Wh is a VS-only shortcut that represents a white space character ([ \t\n\r] -- there might be a few others, too)

If you haven't caught my drift, yet, to sum it all up, I'd like to have an optional identifier. Hopefully, the question mark.

Ratings

Comments

By Michael Flanakin @ Wednesday, November 01, 2006 12:46 PM
I posted a question to an internal mailing list in regards to the optional identifier and had a short discussion with Chris Flaat, who is a lead developer on the Visual Studio team. Chris suggested the usage of the 0-to-many identifier, as I mentioned, but definitely understood that it wouldn't be a panacea for some of the more complicated/strict searches. Ultimately, Chris passed the request on to the feature owners. It's nice to see the wheels in motion, even if it is for something small. We'll see how long it takes to get it into the product.

Click here to post a comment



Archives Archives

Categories Categories

Related Links Related Links