本帖最后由 ssawgift 于 2012-11-20 17:33:04 编辑

解决方案 »

  1.   

    Regex reg=new Regex(@"(?s)\$\{[^{}$]+\}?");
      

  2.   

    前面的回答都达不到要求大概要用到look ahead group,但是具体怎么用,有哪位大侠说下?
      

  3.   

    从你给的示例上来看 2L和4L都可的呀  string s = "${key/setting} string, ${bad ${good/setting}, ${another bad";
                Regex re = new Regex(@"\$\{[^$}]+\}?"); 
                MatchCollection mc = re.Matches(s);
                foreach (Match  c in mc)
                {
                    MessageBox.Show(c.ToString().Trim());
                    System.Diagnostics.Debug.Print(c.ToString().Trim());
                } 
                //输出
                //${key/setting}
                //${bad
                //${good/setting}
                //${another bad
      

  4.   

    对于这个输入:
    this is ${another {$bad example followed by ${good/example}我想如下结果:
    ${another {$bad example followed by (space)
    ${good/example}达不到要求
      

  5.   

    注意输入字符串里含有{$bad,不是${bad。
      

  6.   

    花了几个小时,我自己搞定了。
            /// <summary>
            /// A loose pattern that tries to match all special token that starts with ${.
            /// </summary>
            /// <res>Pay attention to the lookahead assertion!</res>
            public const string L_TOKEN_PATTERN = @"\${(?:(?!\${)[^}])*}?";
      

  7.   

    (?'open'\${).*?(?'-open'(?(\s\$)\s|(?=[}\n])}?))