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?

Ian Patrick Hughes
  • 10,386
  • 3
  • 31
  • 37

6 Answers6

18

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
  • 13,337
  • 3
  • 45
  • 64
  • For frequently I use RegEx, you would think I would already know that. Thanks! – Ian Patrick Hughes Oct 13 '08 at 21:00
  • Also, be careful to test it first; some versions of VS have an off-by-one error in the RegEx replace function. – Nick Oct 13 '08 at 21:18
  • 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 Oct 13 '08 at 21:20
  • Awesome. I didn't know you could do this! – JasonS Oct 13 '08 at 21:20
  • 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 Oct 13 '08 at 22:26
  • 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. – Amanda Mitchell Oct 16 '08 at 21:23
4

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
  • 1,576
  • 1
  • 11
  • 11
  • 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 Oct 13 '08 at 20:55
  • wouldn't HttpContext.Current.Session[“.@”] be a little better? – hova Oct 13 '08 at 20:56
4

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.

Amanda Mitchell
  • 2,665
  • 1
  • 16
  • 23
  • 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 Oct 13 '08 at 21:16
2

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'

Community
  • 1
  • 1
lightmotive
  • 520
  • 5
  • 17
0

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

Degvik
  • 3,050
  • 6
  • 25
  • 20
0

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
  • 32,337
  • 7
  • 60
  • 92