Ian Patrick Hughes
Ian Patrick Hughes

Reputation: 10426

Global Find and Replace in Visual Studio

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?

Upvotes: 5

Views: 5194

Answers (6)

lightmotive
lightmotive

Reputation: 520

None of these answers seem to work in Visual Studio 2013, as that version seems to have finally made the switch to standard RegEx. However, those who are non-RegEx Experts or those who are used to the old VS Find/Replace RegEx syntax will find the RegEx Shortcut buttons very useful.

Please see this answer for more information, including Find/Replace/Surround With examples:

Visual Studio 'Find and Surround With' instead of 'Find and Replace'

Upvotes: 2

DOK
DOK

Reputation: 32831

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.

Upvotes: 0

Amanda Mitchell
Amanda Mitchell

Reputation: 2685

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.

Upvotes: 4

IAmCodeMonkey
IAmCodeMonkey

Reputation: 1568

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

Upvotes: 4

Gordon Bell
Gordon Bell

Reputation: 13633

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()

Upvotes: 18

Degvik
Degvik

Reputation: 3180

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

Upvotes: 0

Related Questions