【发布时间】: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="\<xs:schema\>(.*)\</xs:schema\>"/>
<substitution expression="hello"/>
<filelist
id=""
dir="${project.build.directory}/f"
files="ServiceInterface.wsdl"/>
</replaceregexp>
</target>
</configuration>
</execution>
</executions>
</plugin>
我们将不胜感激获得正确正则表达式的任何帮助。
【问题讨论】:
-
真正的问题是什么?顺便说一句,如果您试图将一个节点插入到倒数第二个位置,显然,该正则表达式肯定不会这样做,因为它会针对
<types>...</types>之间的所有内容 -
@sweaver2112 问题在于获取正确的正则表达式。我编辑了这个问题试图澄清它。我知道我有一个不起作用,我花了很多时间进行测试,但留下了一个简单的例子。我很害怕我的正则表达式技能非常有限。
-
所以你想定位到最后一个模式标记之前的位置?
-
@sweaver2112 是的,并插入与第一个到第二个 xml 块不同的元素。
-
您可以尝试
(?=<xs:schema>(?:(?!<\/xs:schema>).)+<\/xs:schema>\s*<\/types>)regex101.com/r/letGLw/2 并用您的子字符串替换“匹配”(这只是由最后一个锚定的位置)。同样,您没有描述条件或目标,而仅给出了一个输入/输出示例,因此很难在这里知道您想要什么。