Maven ant 正则表达式插入 xml 文本

maven ant regex to insert xml text

我正在尝试通过 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>

如能帮助我们获得正确的正则表达式,我们将不胜感激。

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

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