【问题标题】:How to replace string between two characters in whole text?如何替换整个文本中两个字符之间的字符串?
【发布时间】:2020-06-26 15:43:07
【问题描述】:

我有以下字符串:

{Name}({Age})

我想得到以下内容:

()

我试过这段代码:

@"\{([^\}]+)\}" Only return {Name}

"({)(.*)(})" Return {Name}({Age}

但它们都没有按我的意愿工作。

如何做到这一点?

【问题讨论】:

标签: c# regex string replace


【解决方案1】:

应该这样做:

class Program
{
    static void Main(string[] args)
    {
        string input = @"{Name}({Age})";

        string output = Regex.Replace(input, @"\{.*?\}", "");

        Console.WriteLine(output); // "()"
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多