Visual Studio : Search Uncommented Code only - visual-studio

Is there any way to restrict the Find/Search to uncommented lines only ?
(Maybe using regex would be a good lead)

Lets say, if you need to search all occurrences of an uncommented text "VPEntity" then try using the following regular expression in Find in files after selecting the Use RegEx option
^((?!//|/\*).)*VPEntity
Hope it works for you

Related

Sublime Text - Exclude comments in search

Every time I search for a function inside of hundreds of files, I see so many matches for comments which have no effect in the code.
Can someone limit Sublime Text's search scope to real code, and exclude comments?
I use Sublime Text 3 for developing a C++ program.
I created a Plugin that search for a given string inside a given scope.
The default scope selector is -comment effectively searching outside of comments. The text to search for is taken from the current selection. The results are presented in the drop-down menu
Basically I combined two API methods:
view.find_all(pattern) that searches for a pattern in the given view.
view.match_selector(position, scope_selecor) that check if the given position is inside the given scope.
You could use regex to find patters matching the regex you give.
Design the regex according to match your.
You can give regex by turning on the 'Regular Expression' flag
Example
You can have this regex to match your case if you want to match alone in single line comments.
^(?!\/\/)([^\/\n]*)YOUR_SEARCH_TERM
If you want to match also in multi line comments use this.
^(?!(\/\/|(\/\*(.|\n)*([^\*])(?=\/))))YOUR_SEARCH_TERM

Visual Studio 2012: How to handle code replacement over multiple lines

I often have to replace multiple lines in multiple documents. In Visual Studio 2012 there seems to be the possibility to put muliple line code in a simple one-line-input-box only.
In the "embedded" search box there is also only a single-line input field:
After inserting the often very long search terms and trying to put a regular expressions for line breaks into it, i have to move horizontally inside a one-line-input-field which isn't comfortable at all.
Is there another way to use the find/replace functionality for replacing muliple lines in Visual Studio 2012?
In order to replace multiple lines first you have to check regular expression option on search window. In following example it's search the
first
second
third
and replace it with
fourth
fifth
sixth
search text : first([\s\n])second([\s\n])third
replace text : fourth$1fifth$2sixth
This worked exceptionally well for me:
http://vlasovstudio.com/sync-block-edit/

Exclude comments when searching in Visual Studio

Is there a way to perform searches (Find / Find in Files) in visual studio that will exclude matches in comments? While sometimes it is useful, other times it is the opposite. For all of the options presented, I figured it would be in there, but I can't find it if it is.
I am using VS 2010/2012 about equally by the way.
Here's the regex that works for me for newer versions of Visual Studio:
^(?![ \t]*//).*your_search_term
Note that the syntax changed as of VS 2012:
Visual Studio 2012 uses .NET Framework regular expressions to find and
replace text. In Visual Studio 2010 and earlier versions, Visual
Studio used custom regular expression syntax in the Find and Replace
windows.
Reference: https://msdn.microsoft.com/en-us/library/vstudio/2k3te2cs(v=vs.110).aspx
you could try the regex as below:
^~(:b*//).*your_search_term
Short explanation:
^ from beginning of line
~( NOT the following
:b* any number of white spaces, followed by
// the comment start
) end of NOT
.* any character may appear before
your_search_term your search term :-)
saw this at another post.
I don't believe it's an option in VS. You could try regular expressions, but those are limited by how creative you can be. It seems like it would be not entirely difficult to search for lines not beginning with // using a regex.
You probably mean any comments, both single- (//) and MULTI-LINE (/**/).
The other answers only deal with single-line-comments. So here comes my VS Code-compatible Regex for both of them at the same time:
(?<!//.*)(?!(/\*).*)YOUR_SEARCH_TERM(?![^/\*]*?\*/)
Explanation:
(?<!//.*) is a negative look-behind against single-line-comments (//)
(?!(/\*).*) invalidates your search term if it is preceded by /*
(?![^/\*]*?\*/) invalidates your search term if it is followed by */ (short of a /* between your search term and itself, because then your search term would be outside of /**/ and you want it to be matched again).
To use it in VS Code, just type Ctrl + F and tick the regex-icon in the search-field popping up (with void being your exemplary search term):

How can I search in Visual Studio and get it to ignore what is commented out?

I am refactoring a C++ codebase in Visual Studio 2005. I'm about half way through this process now and I've commented out a lot of old code and replaced or moved it. Now I'm searching to see that I have to change next but the search function keeps bringing me the old commented out stuff I no longer care about. I don't really want to delete that old code yet, just in case.
Is there any way I can search all files in the solution and get results ignoring what is commented out? I don't see a way in visual studio itself, is the perhaps a plug-in that would do it?
As the other provided solutions didn't work for me, I finally discovered the following solution:
^~(:b*//).*your_search_term
Short explanation:
^ from beginning of line
~( NOT the following
:b* any number of white spaces, followed by
// the comment start
) end of NOT
.* any character may appear before
your_search_term your search term :-)
Obviouly this will only work for // and ///-style comments.
You must click "Use Regular Expressions " Button (dot and asterisk) on your find window to apply regex search
In newer versions of visual studio .net regex is used which has a slightly different syntax:
^(?![ \t]*//).*your_search_term
My take:
yes you can use regular expressions, those tend to be too slow and thinking about them distracts from focusing on real stuff - your software.
I prefer non-obtrusive semi-inteligent methods:
Poor man's method:
Find references if you happen to use intelisense on
Or even better:
Visual assist and it's colored "Find all References" and "Go To" mapped to handy shortcuts. This speeds up navigation tremendously.
If you comment your old code with // you can use regular expressions while searching for something in your codebase. Something like this for example: ^[^/][^/].*your_function_name.*.
Previous answer gave a false-positive on cases where otherwise matching lines were placed on lines containing other source:
++i; // your_search_term gets found, don't want it found
So replaced the :b* with .* and added the <> so only entire words are found, and then went after some of the older C-style comments where there's a /* on the line:
^~(.*//)~(.*/\*).*<your_search_term>
In my case I was hunting for all instances of new, not amenable to refactor assistance, and boatloads of false-positives. I also haven't figured out how to avoid matches in quoted strings.
Just to add on, as I was doing a "find all" for division operator used in the code, used the below to exclude comments as well as </ and /> from aspx files:
^~(.*//)~(.*/\*)~(.*\<\/)~(.*/\>).*/
In Visual Basic within Visual Studio 2015, I was able to search for text outside of comments by adapting glassiko's comment from the most upvoted answer
^(?![ \t]*[']).*mysearchterm
And in C# you would use glassiko's comment exactly as it was
^(?![ \t]*//).*mysearchterm
Better use \s I think. ^(?![\s]*//).*your_search_term
delete the commented out code, it is in source control right? there is no need to keep it in the file as well.

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?
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()
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
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.
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'
You can use Visual Assist for tasks like this. It's a powerful tool for different kinds of refactoring.
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.

Resources