Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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# 从带有前导自定义变量的字符串中获取括号值 我想得到一个自定义变量后面括号中的字符串 例如: 我有一个字符串:“这是一个测试(我想从中得到括号:$variable(HeyGuys))” 我只想得到$variable后面括号中的字符串:“HeyGuys” 我尝试了两种不同的方法,但都有相同的结果: //Input this.GetBracketValue("I want to get the brackets from this: $variable(HeyGuys)).", "$variable"); //What the output should look like: "HeyGuys" //What it looks like with what I tried: "HeyGuys)" // Version with regex public string GetBracketValue(string message, string variable) { Regex regex = new Regex("\\" + variable + "\\(.*\\)"); string variable = regex.Match(message).Value; regex = new Regex("\\(.*\\)"); foreach(Match match in regex.Matches(variable)) { string result = match.Value.Remove(0, 1); result = result.Remove(result.Length - 1); return result; } return ""; } // Version without regex public string GetBracketValue(string message, string variable) { int index = message.IndexOf(variable) + 1; string substring = ""; if (index > 0) { substring = message.Substring(index); index = substring.IndexOf("("); if (index > 0) { substring = substring.Substring(substring.IndexOf("(")); substring = substring.Remove(0, 1); } index = substring.LastIndexOf(")"); if (index > 0) { substring = substring.Remove(substring.LastIndexOf(")")); } } return substring; }_C#_Regex - Fatal编程技术网

C# 从带有前导自定义变量的字符串中获取括号值 我想得到一个自定义变量后面括号中的字符串 例如: 我有一个字符串:“这是一个测试(我想从中得到括号:$variable(HeyGuys))” 我只想得到$variable后面括号中的字符串:“HeyGuys” 我尝试了两种不同的方法,但都有相同的结果: //Input this.GetBracketValue("I want to get the brackets from this: $variable(HeyGuys)).", "$variable"); //What the output should look like: "HeyGuys" //What it looks like with what I tried: "HeyGuys)" // Version with regex public string GetBracketValue(string message, string variable) { Regex regex = new Regex("\\" + variable + "\\(.*\\)"); string variable = regex.Match(message).Value; regex = new Regex("\\(.*\\)"); foreach(Match match in regex.Matches(variable)) { string result = match.Value.Remove(0, 1); result = result.Remove(result.Length - 1); return result; } return ""; } // Version without regex public string GetBracketValue(string message, string variable) { int index = message.IndexOf(variable) + 1; string substring = ""; if (index > 0) { substring = message.Substring(index); index = substring.IndexOf("("); if (index > 0) { substring = substring.Substring(substring.IndexOf("(")); substring = substring.Remove(0, 1); } index = substring.LastIndexOf(")"); if (index > 0) { substring = substring.Remove(substring.LastIndexOf(")")); } } return substring; }

C# 从带有前导自定义变量的字符串中获取括号值 我想得到一个自定义变量后面括号中的字符串 例如: 我有一个字符串:“这是一个测试(我想从中得到括号:$variable(HeyGuys))” 我只想得到$variable后面括号中的字符串:“HeyGuys” 我尝试了两种不同的方法,但都有相同的结果: //Input this.GetBracketValue("I want to get the brackets from this: $variable(HeyGuys)).", "$variable"); //What the output should look like: "HeyGuys" //What it looks like with what I tried: "HeyGuys)" // Version with regex public string GetBracketValue(string message, string variable) { Regex regex = new Regex("\\" + variable + "\\(.*\\)"); string variable = regex.Match(message).Value; regex = new Regex("\\(.*\\)"); foreach(Match match in regex.Matches(variable)) { string result = match.Value.Remove(0, 1); result = result.Remove(result.Length - 1); return result; } return ""; } // Version without regex public string GetBracketValue(string message, string variable) { int index = message.IndexOf(variable) + 1; string substring = ""; if (index > 0) { substring = message.Substring(index); index = substring.IndexOf("("); if (index > 0) { substring = substring.Substring(substring.IndexOf("(")); substring = substring.Remove(0, 1); } index = substring.LastIndexOf(")"); if (index > 0) { substring = substring.Remove(substring.LastIndexOf(")")); } } return substring; },c#,regex,C#,Regex,请尝试以下操作: string input = "This is a test (I want to get the brackets from this: $variable(HeyGuys))."; string pattern = @"\$variable\((?'variable'[^\)]+)"; Match match = Regex.Match(input, patte

请尝试以下操作:

            string input = "This is a test (I want to get the brackets from this: $variable(HeyGuys)).";
            string pattern = @"\$variable\((?'variable'[^\)]+)";

            Match match = Regex.Match(input, pattern);
            string variable = match.Groups["variable"].Value;   
请尝试以下操作:

            string input = "This is a test (I want to get the brackets from this: $variable(HeyGuys)).";
            string pattern = @"\$variable\((?'variable'[^\)]+)";

            Match match = Regex.Match(input, pattern);
            string variable = match.Groups["variable"].Value;   
你可以尝试:

\$\w+\((\w+)\)
对上述正则表达式的解释:

您可以在中找到上述正则表达式的演示

C#中的示例实现

您可以在

中找到上述实现的示例运行,您可以尝试:

\$\w+\((\w+)\)
对上述正则表达式的解释:

您可以在中找到上述正则表达式的演示

C#中的示例实现


您可以在

What is
commandVariable
和What is
response
中找到上述实现的示例运行?为什么这么多的困惑。正则表达式没有混淆->
(?我在那里输入了一个错误。现在更改了它为什么
foreach
,你只是在第一次匹配后返回?我的意思是这没关系,但很难判断你是否打算返回第一个字符串结果或字符串值数组。这取决于你,但有很多方法可以做到这一点,包括捕获集合。我只是说你不清楚关于正在完成的工作。真的太糟糕了,你越清楚,你得到的答案就越好。为什么要接受更多的含糊不清?根据你选择的,困难的部分现在取决于你。什么是
commandVariable
,什么是
response
?为什么这么多的混淆。Regex没有混淆->
(?我在那里输入了一个错误。现在更改了它为什么
foreach
,你只是在第一次匹配后返回?我的意思是这没关系,但很难判断你是否打算返回第一个字符串结果或字符串值数组。这取决于你,但有很多方法可以做到这一点,包括捕获集合。我只是说你不清楚关于正在完成的事情。真的太糟糕了,你越清楚,你得到的答案就越好。为什么要接受更多的含糊不清?根据你选择的,困难的部分现在取决于你。