Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式:如何删除所有;R.G(*******)”;一串_C#_.net_Regex_Expression - Fatal编程技术网

C# 正则表达式:如何删除所有;R.G(*******)”;一串

C# 正则表达式:如何删除所有;R.G(*******)”;一串,c#,.net,regex,expression,C#,.net,Regex,Expression,有几个字符串,我想从这些字符串中删除所有“R.G(***)”。例如: 1、 原始字符串: Push("Command", string.Format(R.G("#{0} this is a string"), accID)); Select(Case(T["AccDirect"]).WhenThen(1, R.G("input")).Else(R.G("output")).As("Direct")); R.G("this is a \"string\"") R.G("this is a (

有几个字符串,我想从这些字符串中删除所有“R.G(***)”。例如:

1、 原始字符串:

Push("Command", string.Format(R.G("#{0} this is a string"), accID));
Select(Case(T["AccDirect"]).WhenThen(1, R.G("input")).Else(R.G("output")).As("Direct"));
R.G("this is a \"string\"")
R.G("this is a (string)")
AppendLine(string.Format(R.G("[{0}] Error:"), str) + R.G("Contains one of these symbols: \\ / : ; * ? \" \' < > | & +"));
R.G(@"this is the ""1st"" string.
this is the (2nd) string.")
结果:

Push("Command", string.Format("#{0} this is a string", accID));
Select(Case(T["AccDirect"]).WhenThen(1, "input").Else("output").As("Direct"));
"this is a \"string\""
"this is a (string)"
AppendLine(string.Format("[{0}] Error:", str) + "Contains one of these symbols: \\ / : ; * ? \" \' < > | & +");
@"this is the ""1st"" string.
this is the (2nd) string."
2、 原始字符串:

Push("Command", string.Format(R.G("#{0} this is a string"), accID));
Select(Case(T["AccDirect"]).WhenThen(1, R.G("input")).Else(R.G("output")).As("Direct"));
R.G("this is a \"string\"")
R.G("this is a (string)")
AppendLine(string.Format(R.G("[{0}] Error:"), str) + R.G("Contains one of these symbols: \\ / : ; * ? \" \' < > | & +"));
R.G(@"this is the ""1st"" string.
this is the (2nd) string.")
结果:

Push("Command", string.Format("#{0} this is a string", accID));
Select(Case(T["AccDirect"]).WhenThen(1, "input").Else("output").As("Direct"));
"this is a \"string\""
"this is a (string)"
AppendLine(string.Format("[{0}] Error:", str) + "Contains one of these symbols: \\ / : ; * ? \" \' < > | & +");
@"this is the ""1st"" string.
this is the (2nd) string."
3、 原始字符串:

Push("Command", string.Format(R.G("#{0} this is a string"), accID));
Select(Case(T["AccDirect"]).WhenThen(1, R.G("input")).Else(R.G("output")).As("Direct"));
R.G("this is a \"string\"")
R.G("this is a (string)")
AppendLine(string.Format(R.G("[{0}] Error:"), str) + R.G("Contains one of these symbols: \\ / : ; * ? \" \' < > | & +"));
R.G(@"this is the ""1st"" string.
this is the (2nd) string.")
结果:

Push("Command", string.Format("#{0} this is a string", accID));
Select(Case(T["AccDirect"]).WhenThen(1, "input").Else("output").As("Direct"));
"this is a \"string\""
"this is a (string)"
AppendLine(string.Format("[{0}] Error:", str) + "Contains one of these symbols: \\ / : ; * ? \" \' < > | & +");
@"this is the ""1st"" string.
this is the (2nd) string."
4、 原始字符串:

Push("Command", string.Format(R.G("#{0} this is a string"), accID));
Select(Case(T["AccDirect"]).WhenThen(1, R.G("input")).Else(R.G("output")).As("Direct"));
R.G("this is a \"string\"")
R.G("this is a (string)")
AppendLine(string.Format(R.G("[{0}] Error:"), str) + R.G("Contains one of these symbols: \\ / : ; * ? \" \' < > | & +"));
R.G(@"this is the ""1st"" string.
this is the (2nd) string.")
结果:

Push("Command", string.Format("#{0} this is a string", accID));
Select(Case(T["AccDirect"]).WhenThen(1, "input").Else("output").As("Direct"));
"this is a \"string\""
"this is a (string)"
AppendLine(string.Format("[{0}] Error:", str) + "Contains one of these symbols: \\ / : ; * ? \" \' < > | & +");
@"this is the ""1st"" string.
this is the (2nd) string."
5、 原始字符串:

Push("Command", string.Format(R.G("#{0} this is a string"), accID));
Select(Case(T["AccDirect"]).WhenThen(1, R.G("input")).Else(R.G("output")).As("Direct"));
R.G("this is a \"string\"")
R.G("this is a (string)")
AppendLine(string.Format(R.G("[{0}] Error:"), str) + R.G("Contains one of these symbols: \\ / : ; * ? \" \' < > | & +"));
R.G(@"this is the ""1st"" string.
this is the (2nd) string.")
结果:

Push("Command", string.Format("#{0} this is a string", accID));
Select(Case(T["AccDirect"]).WhenThen(1, "input").Else("output").As("Direct"));
"this is a \"string\""
"this is a (string)"
AppendLine(string.Format("[{0}] Error:", str) + "Contains one of these symbols: \\ / : ; * ? \" \' < > | & +");
@"this is the ""1st"" string.
this is the (2nd) string."
请帮助。

试试这个:

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替换您的整个比赛。这有效地删除了所有不属于这些匹配的部分,即“R.G(*)部分


请注意,如果字符串中某处包含“R.G”或右括号,您将遇到问题,但根据您的输入数据,这可能足以解决问题。

使用此方法,捕获组0是您的目标,组1是您的替代对象

示例作用于#2和#4字符串以及一个新的边缘大小写
R.G(“这是一个(\'string')”)


R.G(…)
项是否总是只存在于一行中?此外,您是否需要能够处理例如
R.G(“Hello”)World“
?对于当前的示例,不应该工作?正则表达式对于此工作是错误的工具。简单的有状态解析器工作得更好。在示例2中,这将失败,因为您使用的是
*
bookendsy不客气。与大多数正则表达式一样,它在边缘情况下也有弱点,因此如果有任何反例,请更新问题。这一个如何:R.G(“这是一个(字符串)”)->“这是一个(字符串)”这一字符串中有()是的,您添加的第四个将是一个挑战。也许我们可以在捕获组1中使用某种前瞻性,以确保它后面紧跟着一个
[^\\]>
或类似的名称。好的,我让
*
变懒了,并确保组书尾不是转义的引号括号序列
\”
前瞻性?我不明白。。。我觉得你真的很好,如果第四场比赛对你来说是一个挑战,我将无法解决它。你能帮我做吗?