有几个字符串,我想从这些字符串中删除所有“R.G( ** )”。例如:
1,原始字符串:
Push("Command", string.Format(R.G("#{0} this is a string"), accID));
结果:
Push("Command", string.Format("#{0} this is a string", accID));
2,原始字符串:
Select(Case(T["AccDirect"]).WhenThen(1, R.G("input")).Else(R.G("output")).As("Direct"));
结果:
Select(Case(T["AccDirect"]).WhenThen(1, "input").Else("output").As("Direct"));
3,原始字符串:
R.G("this is a \"string\"")
结果:
"this is a \"string\""
4,原始字符串:
R.G("this is a (string)")
结果:
"this is a (string)"
5,原始字符串:
AppendLine(string.Format(R.G("[{0}] Error:"), str) + R.G("Contains one of these symbols: \\ / : ; * ? \" \' < > | & +"));
结果:
AppendLine(string.Format("[{0}] Error:", str) + "Contains one of these symbols: \\ / : ; * ? \" \' < > | & +");
6,原始字符串:
R.G(@"this is the ""1st"" string.
this is the (2nd) string.")
结果:
@"this is the ""1st"" string.
this is the (2nd) string."
请帮助。
答案 0 :(得分:1)
使用此选项,捕获组0是您的目标,组1是您的替换。
R[.]G[(]"(.*?[^\\])"[)]
对#2和#4字符串以及新边缘案例R.G("this is a (\"string\")")
var pattern = @"R[.]G[(]\""(.*?[^\\])\""[)]";
var str = "Select(Case(T[\"AccDirect\"]).WhenThen(1, R.G(\"input\")).Else(R.G(\"output\")).As(\"Direct\"));";
var str2 = "R.G(\"this is a (string)\")";
var str3 = "R.G(\"this is a (\\\"string\\\")\")";
var res = Regex.Replace(str,pattern, "\"$1\"");
var res2 = Regex.Replace(str2,pattern, "\"$1\"");
var res3 = Regex.Replace(str3,pattern, "\"$1\"");
答案 1 :(得分:0)
试试这个:
var result = Regex.Replace(input, @"(.*)R\.G\(([^)]*)\)(.*)", "$1$2$3");
说明:
(.*) # capture any characters
R.G\( # then match 'R.G.'
([^)]*) # then capture anything that isn't ')'
\) # match end parenthesis
(.*) # and capture any characters after
$ 1 $ 2 $ 3用捕获组1,2和3替换你的整个匹配。这有效地删除了那些不属于那些匹配的所有内容,即“RG( * )”部分
请注意,如果您的字符串在某处包含“R.G”或右括号,则会遇到问题,但根据您的输入数据,这可能会很好地解决问题。