【发布时间】:2019-08-21 22:40:57
【问题描述】:
需要帮助找出正确的正则表达式以将 xml 标记替换为文件的内容。
尝试了一些基本的东西,比如转义特殊字符,但没有运气。愿意使用 sed 以外的其他东西。
config.txt
<localReplications/>
用config.txt替换
<localReplications>
<localReplication>
<enabled>true</enabled>
<cronExp>0 0 /5 * * ?</cronExp>
<syncDeletes>true</syncDeletes>
<syncProperties>true</syncProperties>
<repoKey>some-repo-key</repoKey>
<url>https://foo.bar/random</url>
<socketTimeoutMillis>15000</socketTimeoutMillis>
<username>foo</username>
<password>bar</password>
<enableEventReplication>true</enableEventReplication>
<syncStatistics>false</syncStatistics>
</localReplication>
</localReplications>
<localReplications/> 标签是非常复杂的 xml 文件的一部分。我希望 <localReplications/> 被替换为 replace-with-config.txt 中的内容
【问题讨论】:
-
不要使用正则表达式来解析和/或修改 XML。使用真正的 XML 解析器,在您的情况下,XSLT 处理器将能够完全满足您的需求。 Perl 具有 XSLT 绑定,如多个地方所述,包括 here
-
我建议使用 XML/HTML 解析器(例如 xmlstarlet)。