【问题标题】:My regex will not work if applied to a multiline xml schema [duplicate]如果应用于多行 xml 模式,我的正则表达式将不起作用 [重复]
【发布时间】:2020-05-01 14:28:44
【问题描述】:

我编写了一个正则表达式来更改一些 XML 节点 - 采用通用节点名称并将其替换为节点属性值,如下所示: 正则表达式

<custom-attribute attribute-id="(.*)?"\s*(>*)(.*?)</custom-attribute>

对于以下 xml:

<custom-attribute attribute-id="isNewtest">false</custom-attribute>

使用替换正则表达式

<$1>$3</$1>

输出

<isNewtest>false</isNewtest>

这是我正在寻找的 - 然而 - 一旦不止一个节点被引入到架构中 - 例如

<custom-attribute attribute-id="isNewtest">false</custom-attribute>
<custom-attribute attribute-id="isAnotherNewtest">false</custom-attribute>

我运行正则表达式,输出为:

<isNewtest">false</custom-attribute>
<custom-attribute attribute-id="isAnotherNewtest>false</isNewtest">false</custom-attribute>
<custom-attribute attribute-id="isAnotherNewtest>

这不是我所期待的,而是我所希望的:

<isNewtest>false</isNewtest>
<isAnotherNewtest>false</isAnotherNewtest>

我显然错过了一次搜索多个节点或多行选择器的内容? - 有人能指出我正确的方向吗?我必须重组一个 20,000 行的 xml 文档,而不是喜欢逐行手动执行它的想法。

【问题讨论】:

  • 这是解析 XML 应该使用专用解析器的原因之一。

标签: regex xml schema


【解决方案1】:

看来我是瞎了,这行得通

<custom-attribute attribute-id="(.*?)"\s*(>)(.*?)</custom-attribute>

【讨论】:

    猜你喜欢
    • 2019-07-25
    • 2015-06-19
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多