开发者

Is it possible to use your match from the match box in the replace box with Regex/Replace?

I have a long list like this

sb.AppendLine(product.product_id.Length.ToString())

I want to use regex search and replace to change every line to something like this

sb.AppendLine("product.product_id " + product.product_id.Length.ToString())

Is this possible to do with regex search and replace?

What do I put in the search box and what to I put in the replace 开发者_JAVA技巧box?

Is it possible to use your match from the match box in the replace box?


Yes it is possible, using capturing parentheses.

I assume that product_id is variable.
Search:

sb\.AppendLine\(product\.(.+)\.Length\.ToString\(\)\)

Replace:

sb.AppendLine("product.$1 " + product.$1.Length.ToString())


in vb .net

Dim str As String str = "sb.AppendLine(product.product_id.Length.ToString())"

Dim RegexObj As Regex RegexObj = New Regex("^sb.AppendLine((.*).Length.ToString())$")

Dim res As String res = RegexObj.Replace(str, "sb.AppendLine(""$1"" + product.product_id.Length.ToString())")

in c# .net

string str = null; str = "sb.AppendLine(product.product_id.Length.ToString())";

Regex RegexObj = default(Regex); RegexObj = new Regex("^sb\.AppendLine\((.*)\.Length\.ToString\(\)\)$");

string res = null; res = RegexObj.Replace(str, "sb.AppendLine(\"$1\" + product.product_id.Length.ToString())");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜