2011-04-15 111 views
1

例如有一些html标签<div id="test"><div><div>testtest</div></div></div></div></div></div>获取HTML几个标签

从这个HTML,我需要得到这个<div id="test"><div><div>testtest</div></div></div>

当前正则表达式/<div id=\"test\">.*(</div>){3}/gim

+1

不要为此使用正则表达式。使用HTML解析器。 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454是原因。你使用什么编程语言? – 2011-04-15 04:12:30

+1

看起来像正则表达式的一个很好的用法。你只需要打开许多相应的关闭,呃?贪婪的比赛并不是你想要的。 – tchrist 2011-04-15 04:17:52

+0

我正在使用ActionScript。我需要精确的3格结束标签。 – Tuco 2011-04-15 04:25:11

回答

1

既然你有特定的需求之间有正则表达式的内容需要正好三个结束标签,这个正则表达式应该做的伎俩:

(<div.*?>)+.*?(</div>){3} 

这里的诀窍是使用懒星(*?)来防止catch-all(。)字符比你想要的更匹配。