我正在使用正则表达式 (Regex),但没有找到准确的输出。 我想找到两个花括号之间的值
{ Value } = value
我使用以下模式但没有得到准确的输出;它不会删除第一个“{”...
string pattern = "\\{*\\}";
如果我的值为{girish}
它返回 {girish
而不是这个我想要girish
作为输出...
最佳答案
令我惊讶的是模式一开始就起作用了——它应该匹配零个或多个大括号。您需要对大括号内的内容进行分组:
string pattern = @"\{([^}]*)\}";
然后提取匹配组的内容。您没有显示您使用什么代码来提取输出,但在这种情况下它将在第 1 组中。例如:
using System;
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
string pattern = @"\{([^}]*)\}";
Regex regex = new Regex(pattern);
string text = "{Key} = Value";
Match match = regex.Match(text);
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
关于.net - 我想找到 { } 之间的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2846922/