Find and Replace on Visual studio with keeping the number - visual-studio

Is there way on visual studio to find and replace text but keeping the number in the string same?
For example, lets say I have a code that saids
fields[0].Value;
fields[1].Value;
And now I would like to replace it with
reader.GetString(0);
reader.GetString(1);
Without manually replacing every single lines of code, I was hoping to do it through find and replace dialog.
Is there any ways of doing this?
Thank you

If you want to replace part of the expression but keep a part (like the number in your case) you can use the search and replace function (ctrl+h) set to use regular expressions (alt+e) and use these expressions:
Search: fields\[(.)\].Value;
Replace: reader.GetString($1);
This will replace all expressions on the form fields[n].Value; with reader.GetString(n); where n is any single character. If you want to restrict it to keep numbers only use fields\[(\d)\].Value;
For more information see: Using Regular Expressions in Visual Studio
I tried it with VS2013 and it worked as expected.

Related

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

So i want to surround all string literals in our C++ source with an _T(...) for our unicode port.
This questions answers how I search for string literals but is there some way of surrounding the matched text with _T() instead of replacing with something else?
I intend to do it one string at a time anyway and not all at once but want to avoid having to type it out or use "Surround With" from Visual Assist myself for each string.
Jochen Kalmbach's answer might work in older versions of Visual Studio, but it didn't work for me in Visual Studio 2013. However, the small RegEx shortcut buttons to the right of the Find/Replace input boxes helped a lot:
In Find, select the ":q Quoted string" option.
In Replace, select the "$1 Substitute the substring matched by captured group number 1", and then surround $1 with _T().
Final Output
Find:
((\".+?\")|('.+?'))
Replace:
_T($1)
Note that the $1 represents the RegEx expression group enclosed in the outermost parentheses.
Here's another example:
Requirement
Find:
Converter.toCustomObject($find("Anything"));
Replace (different Converter method and add parameter after $find() parameter):
Converter.toDifferentObject($find("Anything"), true);
Solution
Find (use RegEx in Find options):
Converter\.toCustomObject\((\$find\(.*)\);
Replace:
Converter.toDifferentObject($1, true);
Notice that the Replace value doesn't need to escape special characters, though you can apply some RegEx, e.g. to add a Line Break after the output, you can use this for Replace:
Converter.toDifferentObject($1, true);\r\n
Goto: Edit|Find and Replace...|Quick Replace..
Then enter:
Find: :q
Replace with : _T(\0)
Use: Regular Expressions

Inserting characters before whatever is on a line, for many lines

I have been looking at regular expressions to try and do this, but the most I can do is find the start of a line with ^, but not replace it.
I can then find the first characters on a line to replace, but can not do it in such a way with keeping it intact.
Unfortunately I donĀ“t have access to a tool like cut since I am on a windows machine...so is there any way to do what I want with just regexp?
Use notepad++. It offers a way to record an sequence of actions which then can be repeated for all lines in the file.
Did you try replacing the regular expression ^ with the text you want to put at the start of each line? Also you should use the multiline option (also called m in some regex dialects) if you want ^ to match the start of every line in your input rather than just the first.
string s = "test test\ntest2 test2";
s = Regex.Replace(s, "^", "foo", RegexOptions.Multiline);
Console.WriteLine(s);
Result:
footest test
footest2 test2
I used to program on the mainframe and got used to SPF panels. I was thrilled to find a Windows version of the same editor at Command Technology. Makes problems like this drop-dead simple. You can use expressions to exclude or include lines, then apply transforms on just the excluded or included lines and do so inside of column boundaries. You can even take the contents of one set of lines and overlay the contents of another set of lines entirely or within column boundaries which makes it very easy to generate mass assignments of values to variables and similar tasks. I use Notepad++ for most stuff but keep a copy of SPFSE around for special-purpose editing like this. It's not cheap but once you figure out how to use it, it pays for itself in time saved.

Visual Studio 2008: Find and Replace with new line character?

Sometimes i would like to search for text containing a new line character and there are other times i would like to replace text with a new line character.
How can i do this with visual studio 2008?
Use a RegEx search:
In the Find Dialog - Expand "Find Options"
Check the box for Use: Regular Expressions
Next to the search box there is now an arrow that is active, it will show you available RegEx options/values.
The value you want will be \n. So "SearchValue\n" should do it.
Be aware that that its not a standard RegEx that you use, it's VS specific.
Replace can also use the RegEx values.
Adding on to Brian Schmitt's answer...
Regular expression searches using \n work as expected. However you have to be a little careful when using \n in regex replaces with Visual Studio 2008. For example, if you search for \n and replace with \n (yes, the exact same thing) all of the line breaks in your file(s) will be converted to Unix-style newlines (LF). This may be a bug in Visual Studio. I find it hard to believe this is the intended functionality.
To get around this, you can use tagged expressions, using curly braces: e.g. search for SearchValue{\n} and replace with ReplaceValue\1. This ensures that the same line-break character(s) that were found when searching will also be used when replacing.
You can try my Multiline Search and Replace Macro.

Regular expression search in files, in the Visual Studio editor

I was trying to search for ".IsSet", but not "DocumentState.IsSet", in VS 2008 using regular expression search. How do I compose the regular expression?
Thanks!
Try
(?!<DocumentState)\.IsSet
The ?!< is a "negative lookbehind".
~(DocumentState)\.IsSet
will match all .IsSet instances that do not follow DocumentState. To match exactly .IsSet but not .IsSetFoo, you can either use
~(DocumentState)\.IsSet>
or check the Match whole word option.
See Regular Expressions (Visual Studio) for a list of regular expression tokens supported in the Visual Studio search.
Try this :
^\.IsSet
^ : means beginning of the string.
I don't know if you can with the VS Search, but you can
Replace DocumentState.IsSet by a token (like "DOCSTATE")
Replace all .IsSert
Replace your token "DOCSTATE" with DocumentState.IsSet

Wildcards to regex in VS find & replace

I need to convert expressions of the form:
return *;
into:
return filter(*);
It seems simple enough to express it with wildcards, however, in visual studio's search & replace dailog, there's no way to associate the first asterisk with the second one. I suppose a regex can do this quite easily, however I know very little about regexes.
How do I express this criteria in regex?
A capture group when searching/replacing with regex in VS can be given by enclosing something with curly braces.
A backreference can be given simply by using \1. There is also a menu to the right of the input fields, containing building blocks.
So you would be simply replacing
return {[^;]+};
by
return filter(\1);
The [^;]+ specifies that you want at least one character that is not a semicolon, so unless you return delegates or anonymous methods this should work fine.

Resources