【问题标题】:maven ant regex to insert xml textmaven ant 正则表达式插入 xml 文本
【发布时间】:2017-05-19 17:31:38
【问题描述】:

我正在尝试通过 maven ant 插件和 replaceregexp 任务将 xml 元素插入到 xml 文件中。

这是它最初的样子:

  <types>
    <xs:schema>
       <xs:import namespace='http://biz.funct.com/c/data/v4_6'
       schemaLocation='commoncomplexelements.xsd' />
    </xs:schema>
    <xs:schema>
       <xs:import namespace='http://biz.funct.com/b/data/v1_5'
       schemaLocation='location.xsd' />
    </xs:schema>
    <xs:schema>
       <xs:import namespace='http://biz.funct.com/location/a/data/v1_5'
       schemaLocation='location-validcodes.xsd' />
    </xs:schema>
    <xs:schema>
       <xs:import namespace='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
       schemaLocation='oasis-200401-wss-wssecurity-secext-1.0.xsd' />
    </xs:schema>  
  </types>

这就是它最终的样子:

  <types>
    <xs:schema>
       <xs:import namespace='http://biz.funct.com/c/data/v4_6'
       schemaLocation='commoncomplexelements.xsd' />
    </xs:schema>
    <xs:schema>
       <xs:import namespace='http://biz.funct.com/b/data/v1_5'
       schemaLocation='location.xsd' />
    </xs:schema>
    <xs:schema>
       <xs:import namespace='http://biz.funct.com/location/a/data/v1_5'
       schemaLocation='location-validcodes.xsd' />
    </xs:schema>
    <xs:schema>
        <xs:import namespace='http://biz.funct.com/location/d/data/v1_5'
            schemaLocation='newfile.xsd' />
    </xs:schema>
    <xs:schema>
       <xs:import namespace='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
       schemaLocation='oasis-200401-wss-wssecurity-secext-1.0.xsd' />
    </xs:schema>  
  </types>

这是我目前所拥有的:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>insert-xml</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target name="Insert xml">
                            <replaceregexp flags="m">
                              <regexp pattern="\&lt;xs:schema\&gt;(.*)\&lt;/xs:schema\&gt;"/>
                              <substitution expression="hello"/>
                              <filelist 
                                  id="" 
                                  dir="${project.build.directory}/f"
                                  files="ServiceInterface.wsdl"/> 
                            </replaceregexp>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我们将不胜感激获得正确正则表达式的任何帮助。

【问题讨论】:

  • 真正的问题是什么?顺便说一句,如果您试图将一个节点插入到倒数第二个位置,显然,该正则表达式肯定不会这样做,因为它会针对 &lt;types&gt;...&lt;/types&gt; 之间的所有内容
  • @sweaver2112 问题在于获取正确的正则表达式。我编辑了这个问题试图澄清它。我知道我有一个不起作用,我花了很多时间进行测试,但留下了一个简单的例子。我很害怕我的正则表达式技能非常有限。
  • 所以你想定位到最后一个模式标记之前的位置?
  • @sweaver2112 是的,并插入与第一个到第二个 xml 块不同的元素。
  • 您可以尝试(?=&lt;xs:schema&gt;(?:(?!&lt;\/xs:schema&gt;).)+&lt;\/xs:schema&gt;\s*&lt;\/types&gt;) regex101.com/r/letGLw/2 并用您的子字符串替换“匹配”(这只是由最后一个锚定的位置)。同样,您没有描述条件或目标,而仅给出了一个输入/输出示例,因此很难在这里知道您想要什么。

标签: java regex xml maven ant


【解决方案1】:

您可以尝试 (?=&lt;xs:schema&gt;(?:(?!&lt;\/xs:schema&gt;).)+&lt;\/xs:schema&gt;\s*&lt;\/type‌​s&gt;) regex101.com/r/letGLw/2 并用您的替换字符串替换“匹配”(它是零宽度 - 仅一个位置,由最后一个 &lt;/types&gt; 锚定)。

https://regex101.com/r/letGLw/2/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多