doupo5178 2010-08-11 08:33
浏览 4
已采纳

如何匹配整个html元素,无论内部是什么,就像浏览器一样?

on a given page there are bunch of elements:

<div class="some class"> <-- here is anything, other divs, even other divs with
the same class, but I need to match right on closing tag for this particular
opening tag --></div>
  • 写回答

4条回答 默认 最新

  • duanlu0386 2010-08-11 09:51
    关注

    Regular expressions describe operations on regular languages. HTML is not a regular language. I'd be prepared to bet you could do it with a so-called "recursive regular expression" as they aren't really regular expressions and aren't limitied to regular languages. I'd be prepared to bet more that you'd be better off parsing it instead anyway.

    The easist (not the best, but the easiest to code in a few lines), is to keep a count of inner divs. Whenever you encounter a div tag, up the count. Whenver you encounter a closing div tag, then drop the count if it's non-zero, or you've found your complete element. Whenever you encounter the end of the file, somebody hasn't closed their divs properly.

    Using an XML parser is easier still if you can either depend on the code being well-formed (if you can't, you've got two problems...) or are prepared to just error in the case of non-well-formed input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效