Replacing old "VariableNames$"

ElderKnight
10-31-2008, 06:53 AM
Are there a couple of regular expressions (or wildcard strings) that I can use in the IDE to replace all instances (in a selected block) of, say, WhatEver$ with strWhatEver?

This would be useful in converting old code systematically.

It's tricky because the $ is itself a special character, and you also have to detect the end of a word. (This also complicated my searching for a solution!)

AtmaWeapon
10-31-2008, 09:50 AM
It would help if VS decided to use the same Regular Expression syntax as .NET for this dialog; instead they went and defined their own "extensions" to the syntax which makes things tougher on people.

I have no idea about the rules for the type suffixes, but I can do an example for what you asked. Assuming that every string variable ends with "$", and you want to convert as you specified, this works:

Find: {:i}\$
Replace With: str\1

Here's why. In the "Find" expression, I use :i, which represents an "identifier" in the special regex language the Find and Replace box uses. An identifier is basically a variable name, though I think in this case it probably works like the Regex character class [a-zA-Z0-9_]. Next, I need to specify that the identifier ends with a $ sign, so I escape it with \ (this is how you escape any special character in a regex.) The "{}" is another extension on top of the normal Regex language; the Find and Replace box uses this to indicate groups*. The reason I used it will be clear when we get to the "replace with" box.

So, in short, "{:i}$" means, "Match any identifier that ends with a $, and store everything but the $ in a group."

Now, let's move to the replacement: str\1
The replacement string has a different syntax than regex. In this case, you get a special feature where for n 0-9 \n represents a group in the "match" box. \0 is defined whether or not you specified groups, and represents the entire match. In this case, \0 would be WhatEver$, which is useless to us. However, I grouped everything but the $, so \1 is WhatEver, which is what we want. I simply prefix this with str\1, and it becomes strWhatEver.

Note that this will preserve capitalization, so if you had whatever$ and wanted to get strWhatever, you can't do this with regular expressions. In this case, you'd do better writing a small tool to go over the file and make these kinds of replacements; you can still use regular expressions to find the strings, but you will want to use custom code for the replacement.

* Normal regex uses () for matching groups. The Find and Replace box still treats () as special characters that must be escaped, but ignores them for grouping. I have no idea why this is so.

ElderKnight
10-31-2008, 10:09 AM
Very nice!

I knew that it would involve backslashes, but I was trying parentheses instead of curly braces, and ">" to find the end of the word, and it wasn't working, needless to say.

Most of my old variable names will need more fixing (they're all getting longer -- in the old days we really had to type them out every time we used one -- as well as more systematic). But this is a good start.

darkforcesjedi
10-31-2008, 10:23 AM
The problem is the above will not work for:

Dim test$
test = "This is a string"

If the variable doesn't appear with its symbol in all places, it gets harder :(

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum