string[] ay=Regex.Matches(yourstr,"(?<=AAA<[李四).*?(?=]>)").Cast<Match>().Select(m=>m.Value).ToArray();

解决方案 »

  1.   


    我这么写  ay里边是空的啊...  可是txt里 确实应该有要求的东西
      

  2.   


    void DoTest()
            {
                #region 输入
                TextBox textbox1 = new TextBox() { Text = "AAA<[" };
                TextBox textbox2 = new TextBox() { Text = "]>" };
                TextBox textInputBox = new TextBox()
                {
                    Text =
                        "AAA<[ 张三******************* ]>AAA<[ 李四******************* ]>AAA<[ 王五******************* ]>"
                };
                #endregion            #region 将输入转义
                string text1 = textbox1.Text, text2 = textbox2.Text;
                if (String.IsNullOrEmpty(text1) || String.IsNullOrEmpty(text2))
                    throw new ArgumentException();
                Dictionary<string, string> changeDic = new Dictionary<string, string>();
                //这里只转义少量的字符
                changeDic.Add("<", "\\<");
                changeDic.Add(">", "\\>");
                changeDic.Add("[", "\\[");
                changeDic.Add("]", "\\]");
                changeDic.ToList().ForEach(x =>
                {
                    text1 = text1.Replace(x.Key, x.Value);
                    text2 = text2.Replace(x.Key, x.Value);
                });
                #endregion            string regexText =text1 + @"[\s\w\*]+" + text2;
                Regex regex = new Regex(regexText);
                MatchCollection mc = regex.Matches(textInputBox.Text);
                mc.Cast<Match>().Select(x =>
                {
                    string str = x.Value;
                    str = str.Remove(0, text1.Length - 1);
                    str = str.Remove(str.Length - text2.Length - 1);
                    return str;
                }).ToArray();
            }
      

  3.   


    你好...  我那个txt文本 字符串不是写死的.  是一较长篇幅。 里边的字符类型 大概是我举例的那样。
      

  4.   

    正则表达式
    https://deerchao.net/tutorials/regex/regex.htm
      

  5.   

    有一个很好的解决办法就是,
    在txt1 里面 和 txt2 里面 都加上一个特殊字符,保证这个字符的唯一性
    截取的时候 从这个特殊字符 到 你想要的内容 ,然后过滤这个字符,
      
    比如:
       textbox1 = AAA<[            textbox1 = textbox1 + "!@#$"              textbox1.substring(你想要的字符串) 
          textbox2 = ]>                   textbox2 = textbox2 + “%&*"                textbox2.substring(你想要的字符串)   这是个好办法,给分啊!
      

  6.   

     var str = "<111>AAA< [ 张三******************* ]>AAA< [ 李四******************* ]>AAA< [ 王五******************* ]>";
                var all = Regex.Matches(str, @"AAA<[\w\W]*?>");  
                foreach (Match item in all)
                {
                    Console.WriteLine(item.Value);
                }            Console.ReadLine();
      

  7.   


    //用这个吧var str = "AAA<111>AAA< [ 张三******************* ]>AAA< [ 李四******************* ]>AAA< [ 王五******************* ]>";
                var  all = Regex.Matches(str, @"AAA<\s*\[\s*[\w\W]*?\s*\]\s*>");  
                foreach (Match item in all)
                {
                    Console.WriteLine(item.Value);
                }            Console.ReadLine();