Visual Studio 2010: pattern based search replace other than regex? - regex

I've taken over a project that is full of code like this:
if (aTraceUserids[t].Trim().ToUpper() == Userid().Trim().ToUpper())
{
// ...
}
What is - using tool-assisted expression formulation - a good way to do a search replace into something like this on a case by case base:
if (aTraceUserids[t].Equals(Userid(), StringComparison.InvariantCultureIgnoreCase))
{
// ...
}
Edit (thanks Dave for making me think on this further):
I know this should be possible with regular expressions, but those are hard to get right and document, so I wonder about tool assisted ways that help me both phrase the expressions and execute them.
Ideally I'm looking for a pattern based search/replace tool that allows me to
enter the search/replace patterns
enter the patterns for the files and directory names to match
visually assists me with the search/replace matches, and allows me to post-edit each occurrence
I don't care much which platform as these kinds of search/replace actions will likely apply to other big code bases as well.
So: any solution based on *nix, Windows or web are fine. CygWin and/or WINE based solutions are fine too. (That's why I removed the VS2010 tag and added some platform tags).

Since this was originally tagged with 'Visual Studio', Visual Studio itself can do regular-expression based find/replace, and the standard 'Find and Replace' dialog will let you pick and choose by hitting 'Find Next', 'Replace' or 'Replace All' as you choose.
For example, I recently changed an API from;
Log.Error(string message, bool someotherArg);
to
Log.Error(string message);
And easily used Visual Studio to replace all usage throughout my codebase of this modified API like so;
Find what;
Log.Error({.*}, true);
Replace with:
Log.Error(\1);
The backquote in the replace line \1 puts the grouped regex contained by {...} into that spot in the replace.
Handy, and built-in. Works for me.

Related

How to change lower case to upper using regular expressions in Visual Studio Code

I'm using Visual Studio Code 1.14.2, and I'm trying to change name of variables to camelCase eg.
set_nominal_wavelength to setNominalWavelength.
Regular expression: _([a-z])
Replace: \U$1\E
does not work. Any idea how to achieve it?
As of vscode v1.75 there is a Transform to Camel Case command. So you could
Find: (_[a-z]+)+
Alt+Enter will select all those
trigger the Transform to Camel Case command
Pretty easy.
In the 1.47 Insiders Build support for the replace case modifiers (\L, \l, \U, \u) has been added to vscode. And so should be in the 1.47 stable release).
So simply doing your find: _([a-z])
and replace with \u$1 (since you only want to capitalize the first letter) works nicely in the Insiders Build now.
Works in both the Find Widget and the Search Panel.
There is a workaround:
Open Replace dialog and enter regex: _([a-z])
Then move focus to the editor area and press Ctrl+F2 ("Change All Occurrences")
Then change case of selection (Ctrl+P >upper)
Then press Left Arrow key and press Delete key
You may use other tools that support change case operators, like Notepad++, sed, R (gsub with perl=TRUE), but VS Code does not support these operators in the replacement pattern.
See this feature request on GitHub:
This is cool to have. This is beyond the scope of what is currently supported by javascript.
We need to come up with our own advanced replace engine to support these cases.

Preserve case during visual studio regex find and replace

I'm trying to find and replace strings using the Visual Studio regex find and replace in some code which includes a lot of inline documentation.
e.g. replace "east" with "north", and "East" with "North".
Since the files contain grammatically correct English right now, I want to be careful not to alter the case of text that may get replaced in the comments.
I know you can turn on the match case, or have one regex for lowercase and one for capitalized words, but I'm wondering if I actually have to do it twice or not (obviously I don't want to).
I've seen other answers for perl and javascript which give language-specfic answers to this question (requiring callbacks), but I'm wondering if it's possible to do just within the visual studio dialog.
If you study Using Regular Expressions in Visual Studio, you will see that there are no such an operator that would keep the case of any specified letter matched/captured with a regex.
In some regex flavors, like in Perl and R (g)sub, you could turn your captures/matches lower/uppercase with a specific operator, but again, it would be a hardcoded action, not keeping the original case intact.
Thus, the only option you have with regex is to run individual search and replace operations (like east --> north and East --> North, maybe with word boundaries around \beast\b to match a whole word).
Else, you need to process the text with some custom code written in some full fledged language.

regex search in vs2010

How can I perform a case insensitive search that will find two words that may or may not have a space between them in Visual Studio 2010 e.g.
Foo Bar
FooBar
fooBar
foo bar
EDIT:
Sorry I should have been clearer - I mean a ctrl+shift+f (Edit -> Find and Replace -> Find in Files) search in VS2010 - I don't want to implement this in code - I want to find code/comments etc that matches the search criteria above.
I could also just do four searches... but I am interested to know how to do this in one go, and I would feel more comfortable doing it in one go as I am refactoring.
Cheers
Rob
Use the regex:
foo:b*bar
In VS search form:
In code Regex has a case sensitivity option.
You could do it with a single regular expression, but I believe the code will be more readable if you handle each case seprately. Check if the string looks like "FooBar" (maybe using a regex), and if it doesn't, just count the number of spaces in the string (you can use Trim()) to eliminate the leading and training spaces).

search in visual studio using regexp

I have a situation where i need to search for couple of objects in my code files. currently i m doing it by seaching using visual studio search option for every text i want to search.
I want to use regular expression ( search -> use -> regular expression ) to search all my text at once using OR operator.
Please suggest me for that, as i am not much familiar with regular expression syntax.
Sry for editing in question itself..
I got the answer. Like if I want to search for objects 'abc','xyz' I would put abc|xyz in visual studio seach box. But i don't know how to make this search case insensitive. I got a hint of using /i or -i or ?i , but where and how - i don't know .
As far as I know, Visual Studio should search case insensitive, unless you check the box that says "Match case" (see screenshot).
You can use the alternation operator | to effectively OR part of the regex. So (foo)|(bar) will find either the text "foo" or the text "bar". Either side, can of course, be a regular expression on its own, so you can come up with some pretty complicated stuff.
But as zzzzBov said, if you want any more help you're going to have to supply more information. Or you could actually, you know, read the documentation.
I've found that the search in file using regular expression is buggy in regard to case sensitivity in Visual Studio 2015. Even with the "Match case" option turned on, it will match text ignoring case by default.
The one trick that somehow fixed this is by using capture, surrounding the literal text you want with parenthesis in addition to the "Match case" option!
So instead of: abc.def
Use: (abc).(def)

Find and replace in files - .NET friendly regular expression syntax

I'm looking for a decent tool that can do search and replace over multiple files, with regular expression syntax I'm use to in C#. Normally I would do this in Visual Studio, except it has the strangest regex syntax (and this is meant to be faster than just replacing the text in the files manually).
So far I've tried windows grep but it didn't like the regex below. The regex in question is
<see cref="(?<class>.+)">(.+)</see>
To replace with
<see cref="${class}"/>
Alternatively converting this to Visual Studio's syntax would be fine!
Jeff has a whole post on this on his blog.
In Visual Studio, find:
\<see cref="{:i}"\>.*\</see\>
and replace with:
<see cref="\1"/>
{} is the VS grouping operator. It is not named. In replacements \n is used for the value of the n-th group in the find expression.
You can tweak the :i and .* parts if you need to account for new lines or other whitespace.
In Visual Studio you can do this with the following Reg Ex:
\<see cref="{.+}"\>.+\</see\>
Replace:
<see cref="\1" />
Remember to select Use/Regular Expressions in the Find and Replace dialog.
I've long been thinking it sucks that Visual Studio does not support the same Regular Expression syntax as the .Net framework.
There are a few choices:
If it's something you do regularly, I would go with PowerGrep as it is probably the most complete and user-friendly tool for this.
Its sibling, RegexBuddy also has a Grep functionality, albeit not as comprehensive.
If you're not afraid of Perl, you can also use its command line as a search and replace tool.
Otherwise, there is still the venerable sed that works on Windows.
notepad++ is quite popular and lightweight and has this functionality, have you tried that?:
http://notepad-plus.sourceforge.net/uk/site.htm
EDIT:
It would appear my link is outdated and this usage is only quite recent:
http://www.opensourcereleasefeed.com/release/show/notepad-v5-2-released-replace-in-files-feature-added
You can use the .NET Regular Expression AddIn to allow you to use .NET regexes with Visual Studio. It's on CodeProject at http://www.codeproject.com/KB/macros/VS2005RegexAddIn.aspx.
Mike