search in visual studio using regexp - regex

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)

Related

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.

Visual Studio 2010: pattern based search replace other than 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.

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).

Visual Studio Find and Replace with Regex

I want to replace C# attributes with VB.NET, which means, [Serializable] should become <Serializable>.
The pattern (\[)(.+)(\]) does find the results but I don't know how to replace the first and the last groups with the appropriate parenthesis.
I read this page, but I didn't understand how to use the curly braces for F&R, I tried to wrap the groups with it but it didn't work.
If you are using the Productivity Power Tools extension from Microsoft that support normal .NET regexes, what you would put in the textbox for the replacement given your regular expression above is:
<$2>
where $2 refers to the second capture group in your regex, i.e. the text between the brackets.
Note that this only works with the Quick Find from Productivity Power Tools though. The normal find/replace in Visual Studio use another syntax altogether.
Find what: \[{Serializable}\]
Replace with: <\1>

Visual Studio 2008 search and replace regex

I have a large solution with a lot of lines that I need to replace.
In Visual Studio, you can search and replace with the aid of regular expressions.
I want to replace lines like:
rst.Fields("CustomerName").Value
rst.Fields("Address").Value
rst.Fields("Invoice").Value
To:
row("CustomerName").ToString()
row("Address").ToString()
row("Invoice").ToString()
Thus keeping the dynamic text part, which can vary.
Is this possible and how?
Update, solution:
Search: rst.Fields{\(.*\)}\.Value
Replace: rst\1.ToString()
Thanks JaredPar!
Try the following
Search Expression: ASpecificCommand(\(.*\))\.ASpecificProperty
Replace Expression: ATotallyDifferentCommand\1.ATotallyDifferentProperty
Note: This is not a perfect solution. Since there are (s involved and hence matching of nested parens, a regex won't ever be a perfect solution. However it should get the job done for the specific pattern you posted
The answer and solution provided helpful in doing a find-replace on messageboxes.
This worked in Visual Studio 2008 (VB .NET):
Example:
MessageBox.Show("Invalid Entry","Error")
Find What:
MessageBox.Show{(.*,*)}
Replace WIth:
Error.ShowError\1\2
Results in:
Error.ShowError("Invalid Entry","Error")
Looks like you have it nailed. It's what is called a "tagged expression" and you can see another example here:
http://blogs.msdn.com/b/zainnab/archive/2010/09/12/replace-in-files-tagged-expressions-vstipfind0016.aspx