views:

1344

answers:

5

I have a solution with multiple projects and we need to do some serious global replacements.

Is there a way to do a wildcard replacement where some values remain in after the replace?

So, for instance if I want every HttpContext.Current.Session[“whatevervalue”] to become HttpContext.Current.Session[“whatevervalue”].ToString() the string value being passed in will be respected? I don’t want to replace “whatevervalue” I just want to append a .ToString() where the pattern matches.

Is this possible in Visual Studio?

+4  A: 

Easy...use regular expressions and grouping.

Find what: (HttpContext.Current.Session[“whatevervalue”])

Replace with: \0.ToString();

Remember to check the Use: and select Regular expressions

IAmCodeMonkey
I must be missing something. I tried a practice run on a selected set of text before posing a question. It seemed to find but not replace correctly. I'll try again, thanks.
Ian Patrick Hughes
wouldn't HttpContext.Current.Session[“.@”] be a little better?
hova
Voted up for use of \0.
Jay Bazuzi
A: 

You can use Visual Assist for tasks like this. It's a powerful tool for different kinds of refactoring.

Delynx
Not for this one.
Lev
+10  A: 

First, Backup your Projects, just in case... Always a good idea before mass replacements.

Then, in the Find/Replace Dialog, select the Use Regular Expressions checkbox:

In the Find box, use the pattern:

HttpContext\.Current\.Session\["{.@}"\]

and in the Replace box, use:

HttpContext.Current.Session["\1"].ToString()
Gordon Bell
For frequently I use RegEx, you would think I would already know that. Thanks!
Ian Patrick Hughes
Also, be careful to test it first; some versions of VS have an off-by-one error in the RegEx replace function.
Nick
If it's possible that your identifiers include embedded quotes (e.g., Session["some \" key"], you'll want to use David Mitchell's regex: http://stackoverflow.com/questions/198984/global-find-and-replace-in-visual-studio#199010
Bradley Grainger
Awesome. I didn't know you could do this!
JasonS
Then off some vote-ups. You were not alone, apparently. :)
Ian Patrick Hughes
Why in the world would anyone have embedded quotes in an identifier? If that's the case though, then just Find: HttpContext\.Current\.Session\[{.@}\] and Replace: HttpContext.Current.Session[\1].ToString()
Gordon Bell
The point is not so much that you might have an embedded quote in a session key, but that people viewing this question may want to replace stuff around strings that do have embedded quotes, so it's better if the answer at the top takes it into account.
David Mitchell
+2  A: 

You want to open the "Find Options" expander and select the "Use Regular Expressions" option. After you've done that, you want these as your find/replace entries:

Find:

HttpContext\.Current\.Session\[{("([^"]|\")*")}\]

Replace:

HttpContext.Current.Session[\1].ToString()

Additional Note:

Once you've enabled regular expressions option, you'll be able to use the right-pointing triangle buttons to access snippets of Visual Studio's Regex syntax.

Also note that Visual Studio's Regex syntax is pretty ghetto, as it hasn't changed since the days of Visual Studio 6 (or earlier?)--so don't take any syntax elements for granted.

For example, one might expect that my find regex above is broken because the backslash before the double-quote is not properly escaped, but in reality, putting a double-backslash there will break the expression, not fix it.

David Mitchell
Thanks for the tip. I have been an avid user of RegEx buddy since Atwood first posted about it a while ago. I am never that surprised by the syntax differences between various RegEx implementations. Thanks for the heads-up!
Ian Patrick Hughes
A: 

You could also consider using the free download tool Refactor available at http://www.devexpress.com/Products/NET/IDETools/RefactorASP/

It does a whole lot more than just find & replace, which they call renaming members with more understandable names. Its various features will easily help you to improve your code.

DOK