1.我要从aspx文件中找出id前缀为"aa_"的<asp:Label>或<asp:Button>控件如:<asp:Label id="aa_1">中国</asp:Label>2.再从上面的例子中把“中国”取出来

解决方案 »

  1.   

    <asp:Label id="aa_.+?">中国</asp:Label>
      

  2.   

    补充一下,可能控件里还有很多个别的属性<asp:Label id="aa_1">中国</asp:Label>
      

  3.   

    string input = "<asp:Label id=\"aa_1\">中国</asp:Label>";
                string regex = "<asp:Label id=\"aa_.+\">([^>]+)</asp:Label>";
                string controlName = Regex.Replace(input, regex, "$1");
      

  4.   

    (?<=<asp:(Label|Button) id="aa_.+?">).*?(?=</asp:(Label|Button)>)
      

  5.   

    如还有其他属性:
    string regex = "<asp:Label id=\"aa_.+\"[^>]*>([^>]+)</asp:Label>";
      

  6.   

    楼上
    可能控件里还有很多个别的属性
    如:<asp:Label id="aa_1"  runat="server">中国</asp:Label>2.从<asp:Label id="aa_1"  runat="server">中国</asp:Label>中
    把中国取出来
      

  7.   

    你没看到我的答案么?
    controlName里存的就是控件名称啊
      

  8.   

    Sorry,答案不严谨,“中国”不能说是控件名称,不过我的方法可以取得“中国”这段内容的
      

  9.   

    从<asp:Label id="aa_1"  runat="server">中国</asp:Label>中-- 麻烦再尽点力,把控件ID取出来。解决后马上结帖
      

  10.   

    string regex = "<asp:Label id=\"(aa_.+)\">([^>]+)</asp:Label>";
                string controlID = Regex.Replace(input, regex, "$1");
                string controlName = Regex.Replace(input, regex, "$2");
      

  11.   

    TO:千里之外不单单是Label类型,也可以是Button
      

  12.   

    晕死,大哥,就20分,有话不能一块儿说阿!
    string regex = "<asp:([^> ]+) id=\"(aa_.+)\">([^>]+)</asp:Label>";
    string controlID = Regex.Replace(input, regex, "$2");
    string controlName = Regex.Replace(input, regex, "$3");
      

  13.   

    "<asp:([^> ]+) --我要做的是,限制几个类型的控件,页不是所有类型的控件
      

  14.   

    现在你可以只写Label和Button两种类型
      

  15.   

    Match m = Regex.Match(input, regex);
    $n 即 m.Group[n].Value
    另外,如果只是几种控件,建议你分几次匹配,这样保证正确并且可以对特定类型控件加一些与其关联的处理代码
      

  16.   

    string regex = "<asp:(controltype1|controltype2|...) id=\"(aa_.+)\">([^>]+)</asp:Label>";
    懂了没?
      

  17.   

    我试了一下,用
                string controlID = Regex.Replace(input, regex, "$1");
                string controlName = Regex.Replace(input, regex, "$2");
    不能取得到控件ID,还有文本值
      

  18.   

    string regex = "<asp:(controltype1|controltype2|...) id=\"(aa_.+)\">([^>]+)</asp:Label>";--你可以测试一下,你这种写法是错误的
      

  19.   

    再试试这个:
                string input = "<asp:type1 id=\"aa_1\">中国</asp:type1> <asp:type2 id=\"(aa_.+)\">([^>]+)</asp:type2>";
                string regex = "<asp:(type1|type2) id=\"(aa_.+)\">([^>]+)</asp:(type1|type2)>";
                for (int i = 0; i < m.Groups.Count; i+=2 )
                {
                    //control i
                    string controlID = m.Groups[i+1].Value;
                    string controlName = m.Groups[i+2].Value;
                }
      

  20.   

    Sorry,贴错了
                string input = "<asp:type1 id=\"aa_1\">中国</asp:type1> <asp:type2 id=\"aa_2\">国</asp:type2>";
                string regex = "<asp:(type1|type2) id=\"(aa_[^>]+)\">([^>]+)</asp:(type1|type2)>";
                Match m = Regex.Match(input, regex);
                for (int i = 0; i < m.Groups.Count; i+=2 )
                {
                    string controlID = m.Groups[i+1].Value;
                    string controlName = m.Groups[i+2].Value;
                    Console.WriteLine(controlID);
                    Console.WriteLine(controlName);
                }
      

  21.   

    刚才想错了,这个经验证通过
                string input = "<asp:type1 id=\"aa_1\">中国</asp:type1> <asp:type2 id=\"aa_2\">国</asp:type2>";
                string regex = "<asp:(type1|type2) id=\"(aa_[^>]+)\">([^>]+)</asp:(type1|type2)>";
                Match m = Regex.Match(input, regex);
                int controlNum = 1;
                while (m.Success)
                {
                    // control i
                    string controlID = m.Groups[2].Value;
                    string controlName = m.Groups[3].Value;
                    controlNum++;
                    m = m.NextMatch();
                }
      

  22.   

    <asp:Button id="aa_3" cssclass="buttoninput" runat="server" Text="登录"></asp:Button>//1.用你的语句不能把上面的控件匹配出来
    2.没有写求控件ID与显示文本的正则等待中
      

  23.   

    <asp:(Label|Button) id="aa_[^>]*>[^<>]*</asp:(Label|Button)>//我自己写出来了
      

  24.   

    <asp:(Label|Button)[^>]*id="aa_[^>]*>(?<getString>\s*|.)*</asp:\1>以上表达式才可以正确获取你所需要的内容1.你的id属性不一定是第一个属性,如果是第二个或第三个则你的表达式无法获取
    2.如果你的标签中间的内容中有"<"或">"或换行 则无法获取
    例如:
    <asp:Label id="aa_1"><中
    国></asp:Label>
    3.如果你的前后标签不一致也会出错
    例如:
    <asp:Button id="aa_1">中国</asp:Label>
    (前面是button 后面是label)<asp:(Label|Button)[^>]*id="aa_[^>]*>(?<getString>\s*|.)*</asp:\1>
    使用这个group["getString"] 就是中间的内容
      

  25.   

    第三点可以不考虑,因为如果前后标签错的话,首先在IDE里就通不过楼上真细心,如果你早点出现,分就给你了
      

  26.   

    能不能写一个把<input id="aa_123" >的控件也取出来?
    就是把id前缀为aa_的<input控件也取出来.得到答案后,我另开帖给50分
      

  27.   

    表再浪费我的感情了答案给你<input\s*id\s*=["']?aa_[^>]*>初步测试成功
      

  28.   

    (?:<asp:(Label|Button)[^>]*id="aa_[^>]*>(?<getString>\s*|.)*</asp:\1>)|(?:<input\s*id\s*=["']?aa_[^>]*>)
      

  29.   

    非常感谢,表再浪费我的感情了答案给你
    //这句话我的理解是不想要分,如果我理解错,请打Error,然后我另开帖给分