简体   繁体   English

替换Visual Studio正则表达式中的文本

[英]Replace text in Visual Studio Regular expression

Recently I convert some of my old codes from vb.net to C# . 最近,我将一些旧代码从vb.net转换为C#

I need to change all my existing codes using visual studio regular expression (find & replace dialog inside Visual studio 2010 IDE) 我需要使用Visual Studio正则表达式更改所有现有代码(在Visual Studio 2010 IDE中查找并替换对话框)

Find

Rows(0).Item(0).ToString()
Replace with 用。。。来代替
 Rows[ 0 ][ 0 ].ToString() 行[ 0 ] [ 0 ] .ToString() 

that means 那意味着

 Rows(-- any --).Item(-- any --).ToString() 行(- 任意 -)。项目(- 任意 -)。ToString()\nto  \nRows[-- any --][-- any --].ToString() 行[- 任意 -] [- 任意 -]。ToString()\n

I searched a lot but I am still not able to do this. 我进行了很多搜索,但仍然无法执行此操作。

Tip: I am using visual studio 2010 提示:我正在使用Visual Studio 2010

My old vb.net code is 我的旧vb.net代码是

var firstColumnValue= dataTable.Rows(0).Item("firstColumn").ToString(); 
var secondColumnValue = dataTable.Rows(0).Item("SecondColumn").ToString();

When i am converting VB.net to C#, i got the following using this plugin, i got this. 当我将VB.net转换为C#时,使用插件得到了以下内容,我知道了。

var firstColumnValue= dataTable.Rows[0]["firstColumn"].ToString(); 
var secondColumnValue = dataTable.Rows[0]["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. 从vb.net到C#的转换工具几乎可以转换所有东西,但是我几乎需要在所有文件中进行上述更改。

Use: 采用:

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

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

and check Use Regular Expression in the search/replace window 并在搜索/替换窗口中选中使用正则表达式

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

You can use Microsoft Visual Studio to convert code from VB to C#. 您可以使用Microsoft Visual Studio将代码从VB转换为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# 您可以使用像这样的免费插件。 此用于Visual Studio 2010/2012的简单插件允许您将VB.net代码转换为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"; 例如:括号内的函数调用,例如"foo bar Rows(0).Item(myfunc(n)).ToString() barfoo";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM