例如下面的标签
<region name=newslist col=4 row=10 order=desc>abcsadf </region>我要去的这个 region 里的所有属性列表
name
col
row
order
已经分别的值属性是随机的,需要获得属性的列表,谢谢,请大家帮忙

解决方案 »

  1.   

    先来个非常规的,如果=后可能会接"..."或'...',说明一下,正则需要修改一下List<string> list = new List<string>();
    string test = "<region name=newslist col=4 row=10 order=desc>abcsadf </region> ";
    Regex.Replace(test, @"(?<=<region\s+([^\s=<>]+=\S+\s*)*)[^\s=<>]+(?==)", delegate(Match m) { list.Add(m.Value); return ""; }, RegexOptions.IgnoreCase);
    foreach (string s in list)
    {
        richTextBox2.Text += s + "\n";
    }
      

  2.   

    正则书写我是新手,楼上的试了,可以得到
    name 
    col 
    row 
    order 我还希望得到他们各自的值呢,谢谢,标签书写很规则,不需要考虑特殊情况,还请朋友完善一些正则
      

  3.   

    常规方法,同样需要楼主把规则明确一下,如有必要,根据规则修改一下正则即可string test = "<region name=newslist col=4 row=10 order=desc>abcsadf </region> ";
    Match m = Regex.Match(test, @"<region\s+((?<pro>[^\s=]+)=\S+\s*)+>", RegexOptions.IgnoreCase);
    for(int i=0;i<m.Groups["pro"].Captures.Count;i++)
    {
        richTextBox2.Text += m.Groups["pro"].Captures[i].Value + "\n";
    }楼主需要明确的内容:
    1、源字符串中只有一个<region...>标签,还是可能有多个
    2、如果有多个,是取到一起,还是每个<region...>标签分开取
    3、只取属性列表,属性对应的值取不取
    4、属性的=后全部是直接接非空格字符串,还是有可能出现"...",'...'的情况
      

  4.   

    1、源字符串中有多个这样的 <region></region>标签
    2、每个 <region...>标签分开取 
    3、属性对应的值也要得到的
    4、属性的=后全部是直接接非空格字符串,非常干净
      

  5.   

    1、源字符串中有多个这样的 <region></region>标签
    2、每个 <region...>标签分开取 
    3、属性对应的值也要得到的
    4、属性的=后全部是直接接非空格字符串,非常干净
      

  6.   


    常规写法string test = "<region name=oldslist col=1 row=2 order=asc>abcsadf </region> jfdsajf  <region name=newslist col=4 row=10 order=desc>abcsadf </region>";
    MatchCollection mc = Regex.Matches(test, @"<region[^>]*>", RegexOptions.IgnoreCase);
    int z = 0;
    foreach (Match m in mc)
    {
        richTextBox2.Text += "第" + ++z + "个region的属性:\n";
        Match mList = Regex.Match(m.Value, @"<region\s+((?<pro>[^\s=]+)=(?<vla>\S+)\s*)+>", RegexOptions.IgnoreCase);
        for (int i = 0; i < mList.Groups[1].Captures.Count; i++)
        {
            richTextBox2.Text += "属性: " + mList.Groups["pro"].Captures[i].Value + "  值: " + mList.Groups["vla"].Captures[i].Value + "\n";
        }
    }
      

  7.   

    这么规矩?
    string raw = "...";//元字符串(包含多个<region>)
    Regex reg = new Regex(@"<region .+?>", RegexOptions.IgnoreCase);
    Match mat = reg.Match(raw);
    List<string[]> list = new List<string[]>();
    while(mat.Success)
    {
    list.Add(mat.Value.Substring(7).Trim().Split(' '));
    mat = reg.Match(raw, mat.Index+mat.Length);
    }
    //list中元素结构:{"name=newslist","col=4"},{"name=newslist","col=4"}...
      

  8.   

    嗯,上面写的有个小bug,最后的属性值会把“>”取进来,再有就是按上面的方法,第二个正则可以简化一下,string test = "<region name=oldslist col=1 row=2 order=asc>abcsadf </region> jfdsajf  <region name=newslist col=4 row=10 order=desc>abcsadf </region>";
    MatchCollection mc = Regex.Matches(test, @"<region[^>]*>", RegexOptions.IgnoreCase);
    int z = 0;
    foreach (Match m in mc)
    {
        richTextBox2.Text += "第" + ++z + "个region的属性:\n";
        Match mList = Regex.Match(m.Value, @"(([^\s=]+)=([^\s>]+)\s*)+", RegexOptions.IgnoreCase);
        for (int i = 0; i < mList.Groups[1].Captures.Count; i++)
        {
            richTextBox2.Text += "属性: " + mList.Groups[2].Captures[i].Value + "  值: " + mList.Groups[3].Captures[i].Value + "\n";
        }
    }或者按照另一种方法来处理,效率会高些
    string test = "<region name=oldslist col=1 row=2 order=asc>abcsadf </region> jfdsajf  <region name=newslist col=4 row=10 order=desc>abcsadf </region>";
    MatchCollection mc = Regex.Matches(test, @"<region[^>]*>", RegexOptions.IgnoreCase);
    int z = 0;
    foreach (Match m in mc)
    {
        richTextBox2.Text += "第" + ++z + "个region的属性:\n";
        MatchCollection mcList = Regex.Matches(m.Value, @"([^\s=]+)=([^\s>]+)", RegexOptions.IgnoreCase);
        foreach (Match mList in mcList)
        {
            richTextBox2.Text += "属性: " + mList.Groups[1].Value + "  值: " + mList.Groups[2].Value + "\n";
        }
    }