Replace text in Visual Studio Regular expression - c#

Recently I convert some of my old codes from vb.net to C#.
I need to change all my existing codes using visual studio regular expression (find & replace dialog inside Visual studio 2010 IDE)
Find
Rows(0).Item(0).ToString()
Replace with
Rows[0][0].ToString()
that means Rows(--any--).Item(--any--).ToString()
to
Rows[--any--][--any--].ToString()
I searched a lot but I am still not able to do this.
Tip: I am using visual studio 2010
My old vb.net code is
Dim firstColumnValue As String = dataTable.Rows(0).Item("firstColumn")
Dim secondColumnValue As String = dataTable.Rows(0).Item("secondColumn")
When i am converting VB.net to C#, i got the following using this plugin, i got this.
var firstColumnValue= dataTable.Rows(0).Item("firstColumn").ToString();
var secondColumnValue = dataTable.Rows(0).Item("SecondColumn").ToString();
But i actually wants like below.
var firstColumnValue= dataTable.Rows[0]["firstColumn"].ToString();
var secondColumnValue = dataTable.Rows[0]["SecondColumn"].ToString();
That vb.net to C# conversion tool converts almost all are fine but i need the above change in almost all files.

Regex:
Rows\(([^)]*)\)\.Item\(([^)]*)\)(\.ToString\(\))
Replacement string:
Rows[\1][\2]\3
DEMO
Example:
string str = "foo bar Rows(0).Item(0).ToString() barfoo";
string result = Regex.Replace(str, #"Rows\(([^)]*)\)\.Item\(([^)]*)\)(\.ToString\(\))", "Rows[$1][$2]$3");
Console.WriteLine(result);
Console.ReadLine();
IDEONE
Find:
Rows\({([^)]*)}\)\.Item\({([^)]*)}\)(\.ToString\(\))
Replace:
Rows[\1][\2]\3

Use:
find : Rows({\d*}).Item({\d*}).ToString()
replace with : Rows[\1][\2].ToString()
and check Use Regular Expression in the search/replace window

You can use Microsoft Visual Studio to convert code from VB to C#.
You can use a free plug-ins like this one This simple plug-in for Visual Studio 2010/2012 allows you to convert VB.net code to C#
Also this regex solutions below are not so perfect. For example: a function call inside parentheses, like "foo bar Rows(0).Item(myfunc(n)).ToString() barfoo";

Related

How to configure Visual Studio to not indent closing parenthesis on multiline function calls?

I'm using Visual Studio 2017 RC (but this probably applies to 2015 and earlier versions as well). I'm working on a C# project where I have lots of code that looks like this:
Ast = new Ast(
CallExpression("add")
.AddChildren(
NumberLiteral("4"),
NumberLiteral("9")
)
)
As you can see, the code takes a nested structure where closing parenthesis match the indentation of the original function call.
My problem is that when my cursor is in between the parentheses of a new function call:
Ast = new Ast([|])
and I hit Enter:
Ast = new Ast(
[|])
there are an extra 4 spaces left behind the closing parens, not matching the original indentation. What I want is this:
Ast = new Ast(
[|])
Is there any way to configure VS to not add those extra spaces when hitting Enter in such a context? Thanks.
You can try this:
Tools->Options->Text Editor->C#->Indenting->Block.

which version of C# to use $ instead of string.format

I find a piece of code like this
int a = 100;
string str = $"{a:0.00}";
Console.WriteLine(str);
the result is "100.00"
The $ have the same function of string.Format, and I want to know which version of C#.
This is called string interpolation, and it's part of C# 6, which was released in July as part of Visual Studio 2015.
A C# 6.0 feature, string interpolation.

Visual Studio change C# method parameter colouring

In Visual Studio 2013, is there a way to change the syntax colouring of C# method parameters?
e.g. Can I have AAA and BBB colored, but not someInt, Foo, ToString
private int MyMethod(int AAA, int BBB)
{
int someInt = new int();
someInt = AAA + BBB;
string Foo = AAA.ToString();
}
I tried going to Tools -> Options -> Environment -> Fonts and Colours -> Text Editor and changing Identifier, but this changed the coloring of just about everything (variables, methods, parameters).
ReSharper can do this.
First, check this in ReSharper options:
Then choose the color in the VS options:
End result:
I recently found this extention while looking for the same thing for TypeScript, apparently it supports C# and VisualBasic, so it may be helpful for you and anybody else looking:
Visual studio SemanticColorizer
For VS 2017 you can use.
Enhanced Syntax Highlighting by Stanislav Kuzmich
https://marketplace.visualstudio.com/items?itemName=StanislavKuzmichArtStea1th.EnhancedSyntaxHighlighting
Now you can do this in Visual Studio without any extension.
Here is VS 2022
Unfortunately, you will not find a way to colorize parameter variables with the C# language. You do have the option, however, of writing an extension to do just that. Or, you could rewrite everything in C++, where you can get your parameters colorized.

Regular expression works in VB but not C#

I have the following regular expression for validating a file name:
^(([a-zA-Z]:|\)\)?(((.)|(..)|([^\/:*\?"\|<>. ](([^\/:*\?"\|<>. ])|([^\/:*\?"\|<>][^\/:*\?"\|<>. ]))?))\)[^\/:*\?"\|<>. ](([^\/:*\?"\|<>. ])|([^\/:*\?"\|<>]*[^\/:*\?"\|<>. ]))?$
I can get it to work in VB.NET but not C#. I can't figure out why it works in one but not the other.
VB code:
Regex.Matches("c:\temp\abc.exe", "^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?))\\)*[^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?$")
C# code:
Regex.Matches("c:\temp\abc.exe", #"^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?))\\)*[^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?$");
As far as I can tell the patterns are identical in both languages with escaping. When I run the VB code I get a match. When I run the C# code I get nothing.
Can anyone see what I'm missing?
Don't you need to also escape the filename in C#? E.g:
#"c:\temp\abc.exe"

Verbatim strings and Visual Studio

Is there a way to get Visual Studio to display strings as verbatim strings (prefixed with '#')? I'd like to easily cut strings containing file paths from Visual Studio into Explorer or other apps.
Clarification: when VS displays a string in the auto, watch, immediate, etc. window, I'd like it to be formatted as as verbatim string so that I can simple copy it for use elsewhere.
You can click on the magnifier in VS 2008 debugger variable display and select "Text Visualizer" which will give you the text in an unmassaged format.
You may need to clarify your question. My first thought was that you just need to prefix the string with '#' to make them verbatim, but you already know that.
string s = #"c:\my folder\";
What exactly are you trying to do with the string?
Here's a discussion on how to actually build verbatim-ized strings:
Can I convert a C# string value to a string literal.
Plugging this in a debug vizualiser is a different story...

Categories