请教高手:正则表达式问题

极地_雪狼 2009-04-09 04:42:26
问一下:
待匹配字符串如下:
<a href="123" class="456" style="789">000</a>
问题:
要分别取出href class style 的值(如果该标签存在)和000
同时href class style 的顺序不是固定的,且不一定存在。
这个正则表达式怎么写?
...全文
54 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
guichuan 2009-04-09
  • 打赏
  • 举报
回复
正则表达式
<a href="(.*?)" class="(.*?)" style="(.*?)">
sky_teamsoft 2009-04-09
  • 打赏
  • 举报
回复
UP
LemIST 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sprc_lcl 的回复:]
C# codestring str = "<a href=\"123\" class=\"456\" style=\"789\">000 </a> ";
string pstr = @"<a( href=""(?<href>[^""]*)""| class=""(?<class>[^""]*)""| style=""(?<style>[^""]*)"")+>(?<atxt>[^<]*)</a>";
Match m = Regex.Match(str, pstr);
if (m.Success)
{
Response.Write(m.Groups["href"].Value + m.Groups["class"].Value + m.Groups["style"].Value);

[/Quote]
Up
极地_雪狼 2009-04-09
  • 打赏
  • 举报
回复
好的。我试试。谢谢先。
sprc_lcl 2009-04-09
  • 打赏
  • 举报
回复
string str = "<a href=\"123\" class=\"456\" style=\"789\">000 </a> ";
string pstr = @"<a( href=""(?<href>[^""]*)""| class=""(?<class>[^""]*)""| style=""(?<style>[^""]*)"")+>(?<atxt>[^<]*)</a>";
Match m = Regex.Match(str, pstr);
if (m.Success)
{
Response.Write(m.Groups["href"].Value + m.Groups["class"].Value + m.Groups["style"].Value);
}

110,530

社区成员

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

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

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