字符串如下:子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6...要拆分成如下有序子串:子串1
子串2
子串3
子串4
子串5
子串6即
每一对<pic_ 和>之间的内容算一个子串
每两对<pic_...>之间如果有内容也算作一个子串应该怎么做

解决方案 »

  1.   

    private void button1_Click(object sender, EventArgs e)
    {
    string s = "子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6";
    string[] haha = Regex.Replace(s, "(<pic_|>){1,}", "|", RegexOptions.IgnoreCase).Split('|');
    }
      

  2.   

    string s = "子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6";
    foreach(Match m in Regex.Matches(s,@"(?<=<pic_|>)[^<>]+"))
    {
        m.Value;//
    }
      

  3.   

    void Main()
    {
    string s="子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6";
    string[]ss=s.Split(new string[]{"<",">","pic_"},StringSplitOptions.RemoveEmptyEntries);
    ss.ToList().ForEach(t=>Console.WriteLine(t));
    }
    /*
    子串1
    子串2
    子串3
    子串4
    子串5
    子串6*/
      

  4.   

    忘记说了,要区分这两种子串,即对应拆分后的子串序列,我还需要建立一个对应的序列来标识子串序列中的元素类型:比如:子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6...要拆分成如下有序子串 以及对应的子串类型子串1 normal
    子串2 pic
    子串3 normal
    子串4 pic
    子串5 pic
    子串6 normal
      

  5.   

    public static void Main()
    {
        string s = "子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6";
        foreach (Match m in Regex.Matches(s, @"(?<=<(pic_)|(>|^))[^<>]+"))
        {
            if (m.Groups[1].Success) Console.WriteLine(m.Value + " - pic");
            else Console.WriteLine(m.Value + " - normal");                
        }
        Console.ReadKey();
    }
    输出
    子串1 - normal
    子串2 - pic
    子串3 - normal
    子串4 - pic
    子串5 - pic
    子串6 - normal
      

  6.   


    假如对于pic类型,也就是<pic_和>之间的子串,我还要屏蔽掉 \/:?*"<>| 这几个字符,且首尾不能为英文句点符,应该怎么写?
      

  7.   

    string pattern1 = "很好很强大<pic_1.2.10.8.\\8_title_shapepic.jpg>很黄很暴力<pic_1.jpg>很傻很天真<pic_2.bmp>我爸是李刚<pic_3.jpeg><pic_4.png><pic_5.gif>xxxxxxx<pic_test.>";比如上面这个字符串解析出来:"1.2.10.8.\\8_title_shapepic.jpg" 这个子串在<pic_和>之间,但是因为其中含有\,所以不符合要求,可以将它标记为一个特殊的类型比如error;还有最后的 "test."因为最后是.号结尾,所以也应该标记为error以上,其实我的目的是做一个图文混排的识别规则,图片以<pic_ 图片文件名.后缀名 >的形式嵌入到文本当中,然后整个一起存到数据库;
    读取的时候识别到<pic_ 图片文件名.后缀名>就根据其中的图片文件名子串到数据库图片表中获取图片,所以 图片文件名中若含有 \/?:*"|<> 等字符都应该识别成非法文件名。
      

  8.   

    恩谢谢。等你忙完再说。我刚接触正则,只会写成这样,也就是能排除\/"*<>:等符号后提取合法<pic_...>;
    提取所有子串并判断分类的话我就不会写,你写的我也不能完全理解,所以你给出答案后最好稍加解释,呵呵
    "<pic_[^\\.]{1}.{1}[^<>\\\\/\\:\\?\\*\"\\|]{1,}[^\\.]{1}>"
      

  9.   

    public static void Main()
    {
        string s = "很好很强大<pic_1.2.10.8.\\8_title_shapepic.jpg>很黄很暴力<pic_1.jpg>很傻很天真<pic_2.bmp>我爸是李刚<pic_3.jpeg><pic_4.png><pic_5.gif>xxxxxxx<pic_test.>";
        foreach (Match m in Regex.Matches(s, @"(?<=<(pic_)|(>|^))(?:[^<>\\/?:*""|]|([\\/?:*""|]))+"))
        {
            if (m.Groups[1].Success)
            {
                if (m.Groups[3].Success)
                {
                    Console.WriteLine(m.Value + "\t[pic - error]");
                }
                else
                {
                    Console.WriteLine(m.Value + "\t[pic]");
                }
            }
            else
            {
                Console.WriteLine(m.Value + "\t[normal]");
            }
        }
        Console.ReadKey();
    }输出很好很强大      [normal]
    1.2.10.8.\8_title_shapepic.jpg  [pic - error]
    很黄很暴力      [normal]
    1.jpg   [pic]
    很傻很天真      [normal]
    2.bmp   [pic]
    我爸是李刚      [normal]
    3.jpeg  [pic]
    4.png   [pic]
    5.gif   [pic]
    xxxxxxx [normal]
    test.   [pic]
      

  10.   


    非常感谢!(?<=<(pic_)|(>|^))(?:[^<>\\/?:*""|]|([\\/?:*""|]))+顺便再请教下:(?<=<(pic_)|(>|^)) ——这是Groups[1]
    (?:[^<>\\/?:*""|]|([\\/?:*""|]))——这是Groups[3]Groups[3]后一个判断条件([\\/?:*""|])有啥用呢?
    另外Groups[2]跑哪里去了?
      

  11.   

    (?<=<(pic_)|(>|^))(?:[^<>\\/?:*""|]|([\\/?:*""|]))+(pic_) Groups[1]
    (>|^)  Groups[2]
    ([\\/?:*""|]) Groups[3]其他的自己仔细理解一下吧。
      

  12.   


    实在不好意思,俺是相当愚钝"(?<=<(pic_)|(>|^))(?:[^<>\\/?:*""|]|([\\/?:*""|]))+"查了不少资料还是不明白,为啥 (?<=<(pic_)|(>|^))不写成 (?<=(<pic_)|(>|^)),以及从<(pic_)和(>|^)这两个零宽断言为啥可以同时捕捉 <pic_和>,以及 >和<,以及>和^,以及&和<之间的所有字符串
      

  13.   

    还有?:是不捕捉的意思吧。
    ?:[^<>\\/?:*""|]|([\\/?:*""|])的意思是不捕捉除了\/?:*"以外的字符,然后又捕捉\/?"*:,这不是自相矛盾耽误你时间不好意思,我愿再补40分
      

  14.   

    可不是吗,还是没有排除.test和test.,自己又改了好久也不对
      

  15.   


    举例有说啊……就是 .号不能在<pic_ 和 >之间的字符串的开头或者结尾
      

  16.   

    规则有点多。没看到那个。你下次吧多个规则按行分开。一行一条。否则容易忘记。public static void Main()
    {
        string s = "很好很强大<pic_1.2.10.8.\\8_title_shapepic.jpg>很黄很暴力<pic_1.jpg>很傻很天真<pic_2.bmp>我爸是李刚<pic_3.jpeg><pic_4.png><pic_5.gif>xxxxxxx<pic_test.>";
        foreach (Match m in Regex.Matches(s, @"(?<=<pic_(?<err>\.)?)(?:[^<>\\/?:*""|]|(?<err>[\\/?:*""|]))+?(?<err>\.)?(?=>)|(?<=>)[^<>]+"))
        {
            if (m.Groups[1].Success)
            {
                if (m.Groups["err"].Success)
                {
                    Console.WriteLine(m.Value + "\t[pic - error]");
                }
                else
                {
                    Console.WriteLine(m.Value + "\t[pic]");
                }
            }
            else
            {
                Console.WriteLine(m.Value + "\t[normal]");
            }
        }
        Console.ReadKey();
    }输出1.2.10.8.\8_title_shapepic.jpg  [pic - error]
    很黄很暴力      [normal]
    1.jpg   [normal]
    很傻很天真      [normal]
    2.bmp   [normal]
    我爸是李刚      [normal]
    3.jpeg  [normal]
    4.png   [normal]
    5.gif   [normal]
    xxxxxxx [normal]
    test.   [pic - error]
      

  17.   


    貌似还是有问题,、、
    1.开头的“很好很强大”   没有捕捉到
    2. 所有正常pic文件全部捕捉到了normal组里
      

  18.   


    汇报下,改成这样,好像暂时没问题了。
    有点困惑的是,把Groups[2]中的(?<=>)改成(?<=(>|^))后,似乎Groups[2]就变成Groups[1]了。
    string regular3 = @"(?<=<pic_)(?<err>\.)?(?:[^<>\\/?:*""|]|(?<err>[\\/?:*""|]))+?(?<err>\.)?(?=>)|(?<=(>|^))[^<]+";foreach (Match m in mc)
    {
    if (m.Groups[1].Success)
    {
    listView1.Items.Add(m.Value);
    listView1.Items[listView1.Items.Count - 1].SubItems.Add("normal");
    }
    else
    {
    if (m.Groups["err"].Success)
    {
    listView1.Items.Add(m.Value);
    listView1.Items[listView1.Items.Count - 1].SubItems.Add("error");
    }
    else
    {
    listView1.Items.Add(m.Value);
    listView1.Items[listView1.Items.Count - 1].SubItems.Add("pic");
    }
    }
    }