Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - How to replace text preserving case

Using the find and replace dialog in Visual Studio (2010) is it possible to replace some text but to preserve the case of the text being replaced.

ie. I want to change 'foo' to 'bar' but in my code I have Foo, foo and FOO. I want the replacement to be Bar, bar, BAR respectively.

Is it possible? I suspect I need to use the regular expression functionality but I need assistance in doing so.

EDIT: I know I can set the match case option, but all that option does is limit the replace to text matching the case of the search term. This is how I am doing it at the moment, but it is tiresome having to do three replacements - foo, Foo and FOO

like image 621
Martin Avatar asked Dec 06 '10 11:12

Martin


2 Answers

It is - simply expand the Find Options area of the Find and Replace dialog and check the Match Case checkbox.

Full documentation on the dialog can be found here:

Match case - Only displays instances of the Find what string that are matched both by content and by case. For example, a search for "MyObject" with Match case selected will return "MyObject" but not "myobject" or "MYOBJECT."


Edit: (following clarification)

I don't know of an easy way to do what you want. A RegEx could possibly be constructed that does this, but I suspect that doing 3 search and replace would be faster, easier and less error prone than a RegEx, in this case.

like image 52
Oded Avatar answered Nov 09 '22 00:11

Oded


I think if you use "match case" = true then you can replace "Foo" to "Bar" and "foo" to "bar"

like image 34
Ives.me Avatar answered Nov 08 '22 22:11

Ives.me