.net - 我想找到 { } 之间的值

标签 .net asp.net regex

我正在使用正则表达式 (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/

相关文章:

c# - .net compact framework 不支持异步调用委托(delegate)

c# - 有没有办法将 aspx 文件放入 Visual Studio 2008 .NET 3.5 的类库中?

c# - 在 C# 中使用 NumericComparer 代码,为什么我的 List.Sort() 会出现转换错误?

.net - 使用 WCF 服务临时 Facading asmx Web 服务

c# - MVC 5 全局用户帐户对象

c# - 如何在 Gridview 中进行数据绑定(bind)?

c# - 在预处理语句 C# 中使用 SqlDBType.Decimal

javascript - 用正则表达式提取字符串

用于验证日期格式 dd/mm/YYYY、dd-mm-YYYY、dd.mm.YYYY、dd mmm YYYY、dd-mmm-YYYY、dd/mmm/YYYY、dd.mmm.YYYY(带闰年支持)的正则表达式

javascript - 正则表达式将特定模式与数字匹配。允许数量 0 且大于 5