Regex re=new Regex(@"\:([^<]+)\<"); 
Match.Groups[0]即是:apple<
Match.Groups[1]即是apple
想取得":"后面所有的字符
\:([^\:]+)

解决方案 »

  1.   

    谢谢,xinbuzailiulei能给我解释一下,这里的groups[0]和groups[1]是怎么回事呢如果下面的取得一行的,又是什么呢
      

  2.   

    string str=@"item:apple <fruit> charater:shine isuooiuiduigudifuod";//取出的是 :和 < 之间的部分
    System.Text.RegularExpressions.Regex re=new System.Text.RegularExpressions.Regex(@".*\:(.*)<.*");
    str = re.Replace(str, "$1", -1);//如果取":"后面所有的字符,将表达式写成".*?\:(.*)$"
      

  3.   

    groups[0]是匹配的整个字符串,groups[1]....groups[n]则是依次匹配的自字符串string str=@"item:apple <fruit> charater:shine isuooiuiduigudifuod";
    Regex re=new Regex(@"\:([^<]+)\<");
    Match oMatch=re.Match(str);
    string oStr="";
    if(oMatch.Success==true){
    GroupCollection oGC=oMatch.Groups;
                     oStr+=oGC[0].Value+"\n";//整个匹配字符串
    oStr+=oGC[1].Value+"\n";//第一个子匹配
        MessageBox.Show(oStr);
    }
             re=new Regex(@"\:([^\:]+)"); 
    oMatch=re.Match(str);
    oStr="";
    while(oMatch.Success==true)
    {
    GroupCollection oGC=oMatch.Groups;
    //oStr+=oGC[0].Value+"\n";//整个匹配字符串
    oStr+=oGC[1].Value+"\n";//第一个子匹配
                    oMatch=oMatch.NextMatch();  

    }
                 MessageBox.Show(oStr);
      

  4.   

    继续感谢还有个问题想一并问了,就是这样一个字符串"?sjdoufdi=====?="我想取出?和?=之间的所有内容Regex re=new Regex(@"?([^?]+)?=);   //因为字符串中间可能出现单独的?或者是=但是不会出现?=我这样写得到的结果只是出现一个=的时候就结束了,这是为什么对上面的那个字符串得到的结果是 sjdoufdi=如果字符串是  "?sa=sjou=ldjouifdidofi?="  得到的是sa=请教怎样才能得到我想要的结果
      

  5.   

    string str="?sjdoufdi=====?=";
            Regex re=new Regex(@"\?([^\?]+)\?\=");
            Match oMatch=re.Match(str);
            string oStr="";
                   if(oMatch.Success==true){
                      GroupCollection oGC=oMatch.Groups;
                      oStr+=oGC[0].Value+"\n";
                      oStr+=oGC[1].Value+"\n";
                      MessageBox.Show(oStr);
                      }