Visual Studio 2008 search and replace regex - 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

Related

How to find with lookahead in Visual studio 2012 Regex Find and Replace

I am trying to find all using statements that are in cs files that contain a reference to a class anywhere in the file following it.
For example, I want to match using Example.Foo; if BaseClass<SomeClass> is found anywhere in the file following it.
Like such:
I've tried (using Example.Foo;)(?=[.\s\n]*BaseClass\<SomeClass\>[.\s\n]*) but no joy...
UPDATE
For clarity, this is for the VS 2012 IDE Find and Replace dialog.
You need to add a (?s), which enables multiline matching, and also escape the period in using Example.Foo. The regex should be something along the lines of:
(?s)using Example\.Foo;(?=.*BaseClass<SomeClass>)
I figured it out myself.
(?s)using Example\.Foo;(?=(.*|\s\r)BaseClass\<SomeClass\>(.*|\s\r))
This does the trick!
Thanks for the (?s) hint, that was required! +1 for that...

To lower case using Find and Replace with regular expression in Visual Studio 2010

I am using the Find and Replace function in Visual Studio 2010 in order to change the coding style for fields.
All instances similar to
m_MyField
should be
_myField
but I can only manage to get
_MyField
using
Find what: m_{[a-zA-Z]}
Replace with: _\1
How do I make the first letter lower case? I have tried the following but it does not work:
Find what: m_{[a-zA-Z]}
Replace with: _\L\1
I found this explained in http://vim.wikia.com/wiki/Changing_case_with_regular_expressions but it only works for vim I think.
Any suggestions are welcomed.
I would use this approach:
Find what: m_A{[a-zA-Z]}
Replace with: _a\1
and repeat it for B..Z
Not elegant but should be done quick.

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>

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