求正则表达式 1.我要从aspx文件中找出id前缀为"aa_"的<asp:Label>或<asp:Button>控件如:<asp:Label id="aa_1">中国</asp:Label>2.再从上面的例子中把“中国”取出来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <asp:Label id="aa_.+?">中国</asp:Label> 补充一下,可能控件里还有很多个别的属性<asp:Label id="aa_1">中国</asp:Label> string input = "<asp:Label id=\"aa_1\">中国</asp:Label>"; string regex = "<asp:Label id=\"aa_.+\">([^>]+)</asp:Label>"; string controlName = Regex.Replace(input, regex, "$1"); (?<=<asp:(Label|Button) id="aa_.+?">).*?(?=</asp:(Label|Button)>) 如还有其他属性:string regex = "<asp:Label id=\"aa_.+\"[^>]*>([^>]+)</asp:Label>"; 楼上可能控件里还有很多个别的属性如:<asp:Label id="aa_1" runat="server">中国</asp:Label>2.从<asp:Label id="aa_1" runat="server">中国</asp:Label>中把中国取出来 你没看到我的答案么?controlName里存的就是控件名称啊 Sorry,答案不严谨,“中国”不能说是控件名称,不过我的方法可以取得“中国”这段内容的 从<asp:Label id="aa_1" runat="server">中国</asp:Label>中-- 麻烦再尽点力,把控件ID取出来。解决后马上结帖 string regex = "<asp:Label id=\"(aa_.+)\">([^>]+)</asp:Label>"; string controlID = Regex.Replace(input, regex, "$1"); string controlName = Regex.Replace(input, regex, "$2"); TO:千里之外不单单是Label类型,也可以是Button 晕死,大哥,就20分,有话不能一块儿说阿!string regex = "<asp:([^> ]+) id=\"(aa_.+)\">([^>]+)</asp:Label>";string controlID = Regex.Replace(input, regex, "$2");string controlName = Regex.Replace(input, regex, "$3"); "<asp:([^> ]+) --我要做的是,限制几个类型的控件,页不是所有类型的控件 现在你可以只写Label和Button两种类型 Match m = Regex.Match(input, regex);$n 即 m.Group[n].Value另外,如果只是几种控件,建议你分几次匹配,这样保证正确并且可以对特定类型控件加一些与其关联的处理代码 string regex = "<asp:(controltype1|controltype2|...) id=\"(aa_.+)\">([^>]+)</asp:Label>";懂了没? 我试了一下,用 string controlID = Regex.Replace(input, regex, "$1"); string controlName = Regex.Replace(input, regex, "$2");不能取得到控件ID,还有文本值 string regex = "<asp:(controltype1|controltype2|...) id=\"(aa_.+)\">([^>]+)</asp:Label>";--你可以测试一下,你这种写法是错误的 再试试这个: 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; } 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); } 刚才想错了,这个经验证通过 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(); } <asp:Button id="aa_3" cssclass="buttoninput" runat="server" Text="登录"></asp:Button>//1.用你的语句不能把上面的控件匹配出来2.没有写求控件ID与显示文本的正则等待中 <asp:(Label|Button) id="aa_[^>]*>[^<>]*</asp:(Label|Button)>//我自己写出来了 <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"] 就是中间的内容 第三点可以不考虑,因为如果前后标签错的话,首先在IDE里就通不过楼上真细心,如果你早点出现,分就给你了 能不能写一个把<input id="aa_123" >的控件也取出来?就是把id前缀为aa_的<input控件也取出来.得到答案后,我另开帖给50分 表再浪费我的感情了答案给你<input\s*id\s*=["']?aa_[^>]*>初步测试成功 (?:<asp:(Label|Button)[^>]*id="aa_[^>]*>(?<getString>\s*|.)*</asp:\1>)|(?:<input\s*id\s*=["']?aa_[^>]*>) 非常感谢,表再浪费我的感情了答案给你//这句话我的理解是不想要分,如果我理解错,请打Error,然后我另开帖给分 两个继承同一个分类窗体,如何区别,如何进行消息传递? 右键菜单如何获取当前的控件 初学~为什么输出一句话中的多个d%是依照后面的数值顺序? 自定义控件在子窗体显示时错位 关于绑定了数据的DATAGRIDVIEW中删除多行数据的问题 关于“.Net精简框架”的模拟键盘问题--100分 为何这个POS打开钱箱的命令不能用? 非C#问题,请高手帮忙! 用vs2005做网站好.用户浏览器版本不同,出现的显示问题! 奇怪,为什么找不着csc.exe文件啊 简单问题 C#中API调用画图的问题
string regex = "<asp:Label id=\"aa_.+\">([^>]+)</asp:Label>";
string controlName = Regex.Replace(input, regex, "$1");
string regex = "<asp:Label id=\"aa_.+\"[^>]*>([^>]+)</asp:Label>";
可能控件里还有很多个别的属性
如:<asp:Label id="aa_1" runat="server">中国</asp:Label>2.从<asp:Label id="aa_1" runat="server">中国</asp:Label>中
把中国取出来
controlName里存的就是控件名称啊
string controlID = Regex.Replace(input, regex, "$1");
string controlName = Regex.Replace(input, regex, "$2");
string regex = "<asp:([^> ]+) id=\"(aa_.+)\">([^>]+)</asp:Label>";
string controlID = Regex.Replace(input, regex, "$2");
string controlName = Regex.Replace(input, regex, "$3");
$n 即 m.Group[n].Value
另外,如果只是几种控件,建议你分几次匹配,这样保证正确并且可以对特定类型控件加一些与其关联的处理代码
懂了没?
string controlID = Regex.Replace(input, regex, "$1");
string controlName = Regex.Replace(input, regex, "$2");
不能取得到控件ID,还有文本值
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;
}
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);
}
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();
}
2.没有写求控件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"] 就是中间的内容
就是把id前缀为aa_的<input控件也取出来.得到答案后,我另开帖给50分
//这句话我的理解是不想要分,如果我理解错,请打Error,然后我另开帖给分