批量查找html中的一行并在其下方添加一些文字
我为此苦苦挣扎了几个小时,但无法让它正常工作。我想做的是找到一行
<div id="content">
并在下面添加几行,其中包含一些变量,例如
<div class="post">
<h2 class="title"><a href="#">**var**</a></h2>
<p class="meta"><span class="date">**var**</span>
<span class="posted">Posted by <a href="#">**var**</a></span></p>
<div style="clear: both;">
</div>
<div class="entry moreless">
**var**
</div>
</div>
是否可能?如果是这样怎么办?
——大卫
I'm struggling with this for few hours, but I can't get it to work properly. What I wanna do is find a line
<div id="content">
and add few lines below that contain some variables like
<div class="post">
<h2 class="title"><a href="#">**var**</a></h2>
<p class="meta"><span class="date">**var**</span>
<span class="posted">Posted by <a href="#">**var**</a></span></p>
<div style="clear: both;">
</div>
<div class="entry moreless">
**var**
</div>
</div>
Is it possible? If so how?
--David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有更好的工具可以解决这个问题。但Windows批处理是可能的!
假设下面的文本是名为 TEMPLATE.HTML 的最外层模板文件。内容触发行可以缩进,但该解决方案不允许触发后有额外的字符。 (这个限制可以解决,但会减慢速度)
下面的示例内容文件(名为 CONTENT_HTML.TXT)包含指定为 !varName! 的变量。并且批处理文件将使用延迟扩展来执行变量替换。这意味着如果您希望内容包含感叹号,则必须以某种方式对其进行编码。有两种选择。一种是使用 HTML 字符表示法 !这对于文本内容来说效果很好,但如果您想要 HTML 注释,则不起作用。对于评论,我定义了一个 !X!将扩展为 !
最后,这是一个批处理文件,它将把所有内容放在一起并创建一个名为 FINAL.HTML 的输出文件。我对变量的值进行了硬编码,但它们可以很容易地作为参数传入。
此解决方案存在一些限制
There are much better tools available to solve this. But it is possible with Windows batch!
Assume the text below is the outermost template file named TEMPLATE.HTML. The content trigger line can be indented, but this solution does not allow extra characters after the trigger. (This limitation could be solved, but it would slow things down)
The sample content file below (named CONTENT_HTML.TXT) contains variables specified as !varName! and the batch file will use delayed expansion to perform the variable substitution. This means that if you want the content to contain an exclamation, it must be encoded somehow. There are two choices. One is to use the HTML character notation ! This works fine for text content, but will not work if you want an HTML comment. For comments I define an !X! variable that will be expanded to !
Finally here is a batch file that will put everything together and create an output file named FINAL.HTML. I hard coded the values for the variables, but they could just as easily be passed in as arguments.
There are some limitations with this solution