Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Replacing old "VariableNames$"


Reply
 
Thread Tools Display Modes
  #1  
Old 10-31-2008, 06:53 AM
ElderKnight ElderKnight is offline
Senior Contributor

Forum Leader
 
Join Date: Oct 2003
Location: Central Florida
Posts: 1,205
Default Replacing old "VariableNames$"


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!)
__________________
-- D.J.

I do not endorse any items advertised within this frame, and regret that the viewer is subjected to such.
Reply With Quote
  #2  
Old 10-31-2008, 09:50 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

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.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 10-31-2008, 10:09 AM
ElderKnight ElderKnight is offline
Senior Contributor

Forum Leader
 
Join Date: Oct 2003
Location: Central Florida
Posts: 1,205
Default

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.
__________________
-- D.J.

I do not endorse any items advertised within this frame, and regret that the viewer is subjected to such.
Reply With Quote
  #4  
Old 10-31-2008, 10:23 AM
darkforcesjedi's Avatar
darkforcesjedi darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,961
Default

The problem is the above will not work for:

Code:
Dim test$
test = "This is a string"
If the variable doesn't appear with its symbol in all places, it gets harder
__________________
To err is human; to debug, divine.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->