内容:尊敬的[张三]先生,你至今为止还欠费[1000]元,请及时缴清!如何有正则表达式把[]括号内的文字取出有逗号连接。谢谢!

解决方案 »

  1.   

    public Regex MyRegex = new Regex(
          "\\[([^\\]]+)\\]",
        RegexOptions.IgnoreCase
        | RegexOptions.CultureInvariant
        | RegexOptions.IgnorePatternWhitespace
        | RegexOptions.Compiled
        );
      

  2.   

    fancyf(凡瑞) 的表达式可以,
    但读出来时不方便,因为他把前者和后者混在一起了
    可以这样string str = "尊敬的[张三]先生,你至今为止还欠费[1000]元,请及时缴清!";
    Regex MyRegex = new Regex(@"\[(?<name>[^\]]+)\].+\[(?<money>[\d]+)\]");
    Match match = MyRegex.Match(str);
    string outstr = match.Groups["name"].Value + "," + match.Groups["money"].Value;
    Console.WriteLine(outstr);结果: 张三,1000