kannan
kannan

Reputation: 91

Replace text in Visual Studio Regular expression

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.

Upvotes: 0

Views: 474

Answers (3)

romabr
romabr

Reputation: 78

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";

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

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

Upvotes: 1

Shai Aharoni
Shai Aharoni

Reputation: 1957

Use:

find : Rows({\d*}).Item({\d*}).ToString()

replace with : Rows[\1][\2].ToString()

and check Use Regular Expression in the search/replace window

Upvotes: 1

Related Questions