Page 1 of 2

嵌套div如何清除的问题

Posted: Jan 09 2008, 15:24
by idn
还是不太理解$NEST,似乎是完全对应的?比如$NEST(<table name=*>,</table>)所对应的每个table中必须有name属性,否则不在范围内。这个“,”是代表中间必须有字符的意思吗?
如下的div
<div id=ad_7605_top>
<div align="center"><img src="/images/loading.gif" align="absmiddle" /></div>
</div>
想清除掉这2个div,我是用如下规则
边界$NEST(<div*>,</div>)
变量<div\s[^>]++id=*ad*</div>
匹配失败
应该如何写才能匹配呢?

Re: 嵌套div如何清除的问题

Posted: Jan 09 2008, 16:19
by phoenix
idn wrote:还是不太理解$NEST,似乎是完全对应的?比如$NEST(<table name=*>,</table>)所对应的每个table中必须有name属性,否则不在范围内。这个“,”是代表中间必须有字符的意思吗?
没错,只有所嵌套的table都符合$NEST()命令中的起始标记表达式才会匹配成功。“,”只是用来在表达式中分割起始标记和结束标记,并无特殊含义。
idn wrote:如下的div
<div id=ad_7605_top>
<div align="center"><img src="/images/loading.gif" align="absmiddle" /></div>
</div>
想清除掉这2个div,我是用如下规则
边界$NEST(<div*>,</div>)
变量<div\s[^>]++id=*ad*</div>
匹配失败
应该如何写才能匹配呢?
当使用$NEST()命令时,匹配表达式必须匹配$NEST()所匹配的所有内容<div\s[^>]++id=*ad*</div>只匹配了以下部分,所以匹配失败:

Code: Select all

<div id=ad_7605_top>
 <div align="center"><img src="/images/loading.gif" align="absmiddle" /></div>
解决方法是在匹配表达式的末尾加个“*”,即改写为<div\s[^>]++id=*ad*</div>*就可以匹配$NEST()所匹配的所有内容了。

所以,在使用$NEST()或$INEST()命令时,多数情况下要在匹配表达式的前后加“*”或“\1”之类的变量以将其撑大。

Re: 嵌套div如何清除的问题

Posted: Jan 10 2008, 11:34
by idn
谢谢!一切ok了 :D

Re: 嵌套div如何清除的问题

Posted: Mar 31 2008, 13:05
by idle.newbie
idn wrote:如下的div
<div id=ad_7605_top>
<div align="center"><img src="/images/loading.gif" align="absmiddle" /></div>
</div>
想清除掉这2个div,我是用如下规则
边界$NEST(<div*>,</div>)
变量<div\s[^>]++id=*ad*</div>
匹配失败
应该如何写才能匹配呢?
那個... 雖然有些遲...

Match式子後面加上*是對的, 也達到了你要的結果. 但是, 要注意的是id=*ad*這裡的第一個*, 就算第一行是<div id=NOTHING>(匹配不到), 第一個*還是會匹配到...loading.gif, 而把兩層都給清掉.
我自己也遇過類似的情況, 還是某次在別的地方發現某些主要內容被清掉, 想了好一陣子才發現.
<div\s[^>]++id=$AV(ad*)[^>]++>* 類似的情況下我可能會用這樣的匹配式, 在需要特定Attribute值時儘量避免在>前使用*而以[^>]++代替. 這或許並不是很好的解法, 但我用起來還可以.

Re: 嵌套div如何清除的问题

Posted: Mar 31 2008, 14:49
by phoenix
idle.newbie wrote:<div\s[^>]++id=$AV(ad*)[^>]++>* 類似的情況下我可能會用這樣的匹配式, 在需要特定Attribute值時儘量避免在>前使用*而以[^>]++代替. 這或許並不是很好的解法, 但我用起來還可以.
精确匹配属性值的关键是使用$AV()。至于用*还是[^>]++则无所谓,除了效率上会稍有区别。

Re: 嵌套div如何清除的问题

Posted: Jul 23 2008, 10:04
by ddbb
我现在也在试这个
搭车问个问题
我想过滤掉这段......

Code: Select all

<div class="clearfix">
  <div class="board-tab-show">
    <ul class="tab">
      <li class="first">内容推荐</li>
      <li><a href="/forums/hot_topics" class="underline">本周热点</a></li>
      <li><a href="/forums/new_topics" class="underline">最新讨论</a></li>
      <li><a href="/forums/my_topics" class="underline">我参与的讨论</a></li>						
    </ul>
    <ul class='adverts'><li><a href='/adverts/110' target='_blank'><span style="color:red;font-weight:bold;">在繁琐中挣扎还是简化自主管理?</span></a></li><li><a href="http://www.javaeye.com/topic/206132">JavaEye问答大赛结束:恭喜 congjl2002 ,hjgundam , ham 获得了前三名!</a></li></ul>
  </div>
  <div class="board-tab-show"><div style="padding-top:10px">
<a href="http://shenyu.javaeye.com/"><img src="http://webmaster.javaeye.com/upload/picture/pic/14807/6f9d90d4-28a5-38ae-a730-9949d23f4136.jpg"> </a>
</div>
</div>
</div>
我定义的是

Code: Select all

Bounds = "$NEST(<div class="clearfix">,</div>)"
Limit = 1100
Match = "<div class="clearfix">*"
然后TEST了一下 发现他只匹配到了 11行那里的</div> 也就是第1个div结束
我给改成了

Code: Select all

Match = "<div class="clearfix">*</div>*"
本以为他会再往后匹配一个div 可是没变 还是只匹配到第1个div结束那......

这是为什么..........
我应该怎么写才能让他匹配全部????

我以前的写法就是数div 然后写 *</div>*</div>*</div>*</div>
难道只能这样么

Re: 嵌套div如何清除的问题

Posted: Jul 23 2008, 13:09
by phoenix
我2楼原来的回帖有误,现已修正。

你看下$INEST()命令解释的红色部分:
$INEST ("Inner Nest") works just like $NEST above except that the initial starting tag and ending tag are located outside the command. In other words, it assumes you've already found the tag your looking for and are only interested in discovering its end.
......
the advantage here is it makes it easier to look for a particular starting tag (in this case a table with "outer" in the name) as opposed to just any starting tag of that type. It would be hard to do this with $NEST alone since any check in the "start match" section would have to be true not only for the outer nested table but the inner ones as well.
即,$NEST()中的起始标记表达式是作用于所嵌套的每一个table的。

关于你给出的例子,即已找到所感兴趣的起始标记,参考蓝色部分,使用$INEST()命令更合适,你试下下面的规则:

Code: Select all

[Patterns]
Name = "$INEST Test"
Active = TRUE
Bounds = "<div class="clearfix">$INEST(<div*>,</div>)</div>"
Limit = 1100
Match = "*"

Re: 嵌套div如何清除的问题

Posted: Jul 23 2008, 14:01
by ddbb
现在可以了........
不过我的问题也来了......
这个bounds match和matching expression有什么区别么
我把

Code: Select all

<div class="clearfix">$INEST(<div*>,</div>)</div>
不管放到哪里都能正常过滤........
想不明白bounds match有什么用

另外顺便再问一下 我想定义URL结束 该怎么写
比如只匹配 http://www.sina.com.cn/
不匹配 http://www.sina.com.cn/abc/
我记得正则应该是 后面加 $ 可我试了好像不行........

Re: 嵌套div如何清除的问题

Posted: Jul 23 2008, 14:31
by phoenix
Bounds match限定matching expression作用的范围,如果你把两者设为一样,自然看不出区别。
大部分情况下,你也可以忽略bounds match,而直接在matching expression中限定匹配范围;但将2者分开会使规则更易阅读和修改,在编写复杂规则的情况下尤为明显。

Code: Select all

www.sina.com.cn(^?)

Re: 嵌套div如何清除的问题

Posted: Jul 23 2008, 14:34
by Ray4
ddbb wrote:现在可以了........
不过我的问题也来了......
这个bounds match和matching expression有什么区别么
我把

Code: Select all

<div class="clearfix">$INEST(<div*>,</div>)</div>
不管放到哪里都能正常过滤........
想不明白bounds match有什么用

另外顺便再问一下 我想定义URL结束 该怎么写
比如只匹配 http://www.sina.com.cn/
不匹配 http://www.sina.com.cn/abc/
我记得正则应该是 后面加 $ 可我试了好像不行........

bounds match只是限定查找的范围,如果这里加了,但matching里留空的话,是不会替换成功的。不信你试试

只匹配 sina首页:

Code: Select all

http://www.sina.com.cn/(^?)
在header filter editor的帮助里有这个的说明
use "(^?)" in the match. Looks odd? Well, it is! Since (^...) means NOT and ? means ANY character, (^?) translates into "not any character"!