【发布时间】:2017-08-22 06:26:50
【问题描述】:
正如我在标题中所说,我的数据集是标记,看起来有点像这样
<!DOCTYPE html>
<html>
<head>
<title>page</title>
</head>
<body>
<main>
<div class="menu">
<img src=mmayboy.jpg>
<p> stackoverflow is good </p>
</div>
<div class="combine">
<p> i have suffered <span>7</span></p>
</div>
</main>
</body>
</html>
我的正则表达式引擎尝试分别匹配以下每个节点块,即我可以尝试匹配combine 或menu。一口气,这就是我的正则表达式引擎的样子,尽管我在它下面深入研究了它的内部。
/(<div class="menu">(\s+.*)+<\/div>(?:(?=(\s+<div))))/
它会尝试深入该标记并获取所需的节点块。就这些。至于内部,我们开始吧
/
(
<div class="menu"> // match text that begins with these literals
(
\s+.*
)+ /* match any white space or character after previous. But the problem is that this matches up till the closing tag of other DIVs i.e greedy. */
<\/div> // stop at the next closing DIV (this catches the last DIV)
(?: // begin non-capturing group
(?=
(
\s+<div
) /* I'm using the positive lookahead to make sure previous match is not followed by a space and a new DIV tag. This is where the catastrophic backtracking is raised. */
)
)
)
/
我用 cmets 缩进了它,以帮助任何愿意提供帮助的人。我还从博客中寻找解决方案,the manual他们说这是由具有太多可能性的表达式引起的,可以通过减少结果的机会来补救,即+?而不是*但是尽我所能,我无法将其中任何一个应用到我目前的困境中。
【问题讨论】:
标签: javascript regex parsing html-parsing