求正则表达式

yjdn 2006-10-03 12:50:27
1.我要从aspx文件中找出id前缀为"aa_"的<asp:Label>或<asp:Button>控件

如:<asp:Label id="aa_1">中国</asp:Label>

2.再从上面的例子中把“中国”取出来
...全文
704 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzdiablo 2006-10-10
  • 打赏
  • 举报
回复
算了 我并不是那么看重分数 只是说说好玩
yjdn 2006-10-09
  • 打赏
  • 举报
回复
非常感谢,

表再浪费我的感情了答案给你
//这句话我的理解是不想要分,

如果我理解错,请打Error,然后我另开帖给分
gzdiablo 2006-10-09
  • 打赏
  • 举报
回复
(?:<asp:(Label|Button)[^>]*id="aa_[^>]*>(?<getString>\s*|.)*</asp:\1>)|(?:<input\s*id\s*=["']?aa_[^>]*>)

gzdiablo 2006-10-08
  • 打赏
  • 举报
回复
<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"] 就是中间的内容
yjdn 2006-10-08
  • 打赏
  • 举报
回复
分出来写,我也会,我想知道的是,和以前的合并在一块
yjdn 2006-10-08
  • 打赏
  • 举报
回复
第三点可以不考虑,因为如果前后标签错的话,首先在IDE里就通不过

楼上真细心,如果你早点出现,分就给你了
gzdiablo 2006-10-08
  • 打赏
  • 举报
回复
表再浪费我的感情了答案给你

<input\s*id\s*=["']?aa_[^>]*>

初步测试成功
yjdn 2006-10-08
  • 打赏
  • 举报
回复
能不能写一个把<input id="aa_123" >的控件也取出来?
就是把id前缀为aa_的<input控件也取出来.

得到答案后,我另开帖给50分
bobo0124 2006-10-03
  • 打赏
  • 举报
回复
<asp:Label id="aa_.+?">中国</asp:Label>
yjdn 2006-10-03
  • 打赏
  • 举报
回复
<asp:(Label|Button) id="aa_[^>]*>[^<>]*</asp:(Label|Button)>

//我自己写出来了

yjdn 2006-10-03
  • 打赏
  • 举报
回复
<asp:Button id="aa_3" cssclass="buttoninput" runat="server" Text="登录"></asp:Button>

//1.用你的语句不能把上面的控件匹配出来
2.没有写求控件ID与显示文本的正则

等待中。。。
Nara 2006-10-03
  • 打赏
  • 举报
回复
刚才想错了,这个经验证通过
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();
}
Nara 2006-10-03
  • 打赏
  • 举报
回复
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);
}
Nara 2006-10-03
  • 打赏
  • 举报
回复
再试试这个:
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;
}
yjdn 2006-10-03
  • 打赏
  • 举报
回复
string regex = "<asp:(controltype1|controltype2|...) id=\"(aa_.+)\">([^>]+)</asp:Label>";

--你可以测试一下,你这种写法是错误的
yjdn 2006-10-03
  • 打赏
  • 举报
回复
我试了一下,用
string controlID = Regex.Replace(input, regex, "$1");
string controlName = Regex.Replace(input, regex, "$2");
不能取得到控件ID,还有文本值
Nara 2006-10-03
  • 打赏
  • 举报
回复
string regex = "<asp:(controltype1|controltype2|...) id=\"(aa_.+)\">([^>]+)</asp:Label>";
懂了没?
yjdn 2006-10-03
  • 打赏
  • 举报
回复
我现在就是想放在一块匹配,并看看这样的正则表达式怎么样写
Nara 2006-10-03
  • 打赏
  • 举报
回复
Match m = Regex.Match(input, regex);
$n 即 m.Group[n].Value
另外,如果只是几种控件,建议你分几次匹配,这样保证正确并且可以对特定类型控件加一些与其关联的处理代码
yjdn 2006-10-03
  • 打赏
  • 举报
回复
现在你可以只写Label和Button两种类型
加载更多回复(14)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧