求一正则表达式

custom1234 2012-11-14 02:01:05


<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>

比如以上html代码片段里,我想取<p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>这段, 其它的不取,请问如何写正则表达式???
...全文
214 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
custom1234 2012-11-20
  • 打赏
  • 举报
回复
该问题最终没有用正则!详见http://bbs.csdn.net/topics/390281495
一起混吧 2012-11-14
  • 打赏
  • 举报
回复
是的,空格不固定,就加上 \s var reg=/<p class="t">\d+<\/p>\s*-\s*<a[^>]+>.+?<\/a>/ig;
custom1234 2012-11-14
  • 打赏
  • 举报
回复
引用 15 楼 jordan102 的回复:
我 #9 的代码是可以的,真不谁知道你想要什么结果。如果要分组,请说清楚想要什么结果。 如果想要 c# 版,那你应该去c# 版块求解。
引用 9 楼 jordan102 的回复:
var s='<span>4564373</span ><br/> <span>45673</span ><br/>\ \<p class="t">22455</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\ \<span>45668973</span ><br/>\ \<p class="t">444566</p> - <a he……
谢谢你的回答,你给的正则也适应我给出的例子数据,按照你9#的代码,有一小小的变化就是横线前后的空格不固定,有多个,有回车等,是不是要加\s呢?? 另外我想将你的正则尽量往我代码靠,但都没执行成功,可能.NET里的正则与js有所不同吧!
一起混吧 2012-11-14
  • 打赏
  • 举报
回复
我 #9 的代码是可以的,真不谁知道你想要什么结果。如果要分组,请说清楚想要什么结果。 如果想要 c# 版,那你应该去c# 版块求解。
custom1234 2012-11-14
  • 打赏
  • 举报
回复
引用 13 楼 danica7773 的回复:
<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a> string p = "<p class=\"t\">(?<content1>.+?)</p>\\s[-]\\s<a href=(?<url>.+?)>(? <content2>.+?) 这你能匹配……
还是不行!
打字员 2012-11-14
  • 打赏
  • 举报
回复
<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a> string p = "<p class=\"t\">(?<content1>.+?)</p>\\s[-]\\s<a href=(?<url>.+?)>(? <content2>.+?) 这你能匹配到才见鬼了,当然,也可能是你写例子时手误,那就当我没说

var reg = (/<p[^>]*class="t">([^<>]+?)<\/p>[\s]*?\-[\s]*?<a[^>]*href="([^"]*?)">([^<>]*?)<\/a>/i);
-布谷鸟- 2012-11-14
  • 打赏
  • 举报
回复
似乎跑偏了!说说你的本意,让大家给你出出主意!比这样省事!
一起混吧 2012-11-14
  • 打赏
  • 举报
回复
不懂C#, 请高手帮忙吧。
custom1234 2012-11-14
  • 打赏
  • 举报
回复
引用 9 楼 jordan102 的回复:
var s='<span>4564373</span ><br/> <span>45673</span ><br/>\ \<p class="t">22455</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\ \<span>45668973</span ><br/>\ \<p class="t">444566</p> - <a he……
谢谢你的回复,你能否配合我这种代码来写呢: string p = "<p class=\"t\">(?<content1>.+?)</p>\\s[-]\\s<a href=(?<url>.+?)>(? <content2>.+?)</a>"; Regex reg = new Regex(p, RegexOptions.IgnoreCase | RegexOptions.Compiled); MatchCollection ms = reg.Matches( myHtml); foreach (Match m in ms) { string url = m.Groups["url"].Value; string content1 = m.Groups["content1"].Value; string content2 = m.Groups["content2"].Value; } 因为我想取括号里组合的值,你写的正则我无法套用或应用,不好意思,麻烦你了,(以上是c#代码)
一起混吧 2012-11-14
  • 打赏
  • 举报
回复
var s='<span>4564373</span ><br/> <span>45673</span ><br/>\ \<p class="t">22455</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\ \<span>45668973</span ><br/>\ \<p class="t">444566</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\ \<div><p class="t">46643</p></div>\ \<p class="t">111444</p> - <a herf="xxxxxx.html">xxxxxxxx</a>\ \<table><tr><td>435678</td></tr></table>'; var ar=new Array(),r=null; var reg=/<p class="t">\d+<\/p> - <a[^>]+>.+?<\/a>/ig; while(r=reg.exec(s)){ ar.push(r[0]); } alert(ar);
custom1234 2012-11-14
  • 打赏
  • 举报
回复
问题补充:以上给出的html代码片段只是一种类似的构造,真实数据源的是一个网页的内容。 一个网页的html内容有很多不固定的标签,但我只能取类似于:<p class="t"></p> - <a herf="xxxxxx.html">xxxxxxxx</a> 这段,即p标签,class等于t的,同时这段与a标签紧凑在一起的,中间有横线隔着,如上面一 样,横线两头可能有空格,我用以下方法取的不正确:(C#代码) string p = "<p class=\"t\">(?<content1>.+?)</p>\\s[-]\\s<a href=(?<url>.+?)>(? <content2>.+?)</a>"; Regex reg = new Regex(p, RegexOptions.IgnoreCase | RegexOptions.Compiled); MatchCollection ms = reg.Matches(myHtml); foreach (Match m in ms) { string url = m.Groups["url"].Value; string content1 = m.Groups["content1"].Value; string content2 = m.Groups["content2"].Value; } 以上代码可以抓取所需要的,但不需要的也抓取了,比如:<p class="t">的也抓取了,我要的 是紧靠着-<a>这个整体的内容。即只想抓取<p class="t">xxxxxx</p> - <a herf="xxxxxx.html">xxxxxxxx</a> 这样的组合内容, 单独的<p class="t">xxxxxx</p> 不抓取!!
  • 打赏
  • 举报
回复
换一个思路吧,不适合正则
custom1234 2012-11-14
  • 打赏
  • 举报
回复
<p class="t">111444</p> - <a herf="xxxxxx.html">xxxxxxxx</a> 我想要取以上这段,其中数值和xxxx是动态的
custom1234 2012-11-14
  • 打赏
  • 举报
回复
<span>4564373</span ><br/> <span>45673</span ><br/> <p class="t">22455</p> - <a herf="xxxxxx.html">xxxxxxxx</a> <span>45668973</span ><br/> <p class="t">444566</p> - <a herf="xxxxxx.html">xxxxxxxx</a> <div><p class="t">46643</p></div> <p class="t">111444</p> - <a herf="xxxxxx.html">xxxxxxxx</a> <table><tr><td>435678</td></tr></table> 以上html也只是一个片段,想说明的是不能用排除替换来做
custom1234 2012-11-14
  • 打赏
  • 举报
回复
引用 1 楼 jordan102 的回复:
var s='<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>'; alert(s.replace(/^<p.+?(?=<p)/,''));
r eg="<p class=\"t\"></p>-<a herf=(?<url>.+?)>(?<content>.+?)</a>"; 我用以上表达式的话无法实现
一起混吧 2012-11-14
  • 打赏
  • 举报
回复
那就贴出的你串
custom1234 2012-11-14
  • 打赏
  • 举报
回复
引用 1 楼 jordan102 的回复:
var s='<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>'; alert(s.replace(/^<p.+?(?=<p)/,''));
<p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>这段存在很多html代码中,不仅仅是我给出这段代码片段
一起混吧 2012-11-14
  • 打赏
  • 举报
回复
var s='<p class="t"></p>xxxxxxxxxxxxxxx <p class="t"></p>-<a herf="xxxxxx.html">xxxxxxxx</a>'; alert(s.replace(/^<p.+?(?=<p)/,''));

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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