【发布时间】:2016-02-09 18:42:36
【问题描述】:
我想使用正则表达式从 html 中提取一些内容并将该内容写入新的 html。示例 HTML 如下:
<html>
<script src='.....'>
</script>
<style>
...
</style>
<div class='header-outer'>
<div class='header-title'>
<div class='post-content'>
<noscript>
<p>content we want</p>
</noscript>
</div>
</div></div>
<div class='footer'>
</div>
</html>
我可以使用 grep 在<div class='post-content'>和</div> 之间选择内容并将内容写入新的 html 吗?所以新的 html 看起来像这样:
<div class='post-content'>
<noscript>
<p>content we want</p>
</noscript>
</div>
我对堆栈溢出做了一些研究,发现了一些可能对我的问题有帮助的代码,比如
grep -L -Z -r "<div class='post-content'>.*?<\/noscript><\/dive>" .| xargs -0 -I{} mv {} DIR?
正确吗?如果是,xargs 部分是什么意思?谢谢您,期待您的回复!
【问题讨论】:
-
使用 GNU grep:
grep -Poz "(?s)<div class='post-content'>.*</div>" file.xml > new.html -
嗨赛勒斯,我试过你的,但不知何故对我不起作用。不过谢谢!