【问题标题】:Pull out replaceable fields from a string [closed]从字符串中提取可替换字段[关闭]
【发布时间】:2013-11-27 17:04:52
【问题描述】:

我有一个字符串,可能包含以下内容:

{狗}跳过{狐}

我需要获取这个字符串并获取一个包含“{dog}”和“{fox}”的数组。
是否有任何正则表达式向导可以帮助我?

【问题讨论】:

  • 您尝试过的方法有什么不妥?
  • 我没有尝试任何东西,因为正则表达式很麻烦。 :)
  • 如果这很麻烦,那我们为什么还要为你做呢? :) StackOverflow 不是代码生成器服务。
  • 因为熟悉正则表达式的人可以提供帮助。

标签: c# regex


【解决方案1】:
    static void Main(string[] args)
    {
        String str = "{dog} and the {cat}";
        String[] ret = ExtractTagValue(str);
    }

    public static String[] ExtractTagValue(String input)
    {
        List<String> retLst = new List<string>();

        String pattern = "(\\{.*?\\})";
        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);

        System.Text.RegularExpressions.MatchCollection matches = regex.Matches(input);
        if (matches.Count > 0)
        {


            foreach (Match match in matches)
            {
                retLst.Add(match.Value);
            }
        }

        return retLst.ToArray();
    }

结果将是:

{狗} {猫}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 2018-05-13
    • 2013-03-08
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    相关资源
    最近更新 更多