我需要将href链接转换为不同类型的链接(Confluence有自己的系统(,我即将使用带有replaceregexp的Ant build.xml文件,但还没有完全实现。
基本上我需要从这样的链接开始:
<a class="xref" href="../Test_Topic_2/Test_Topic_2.txt">Test Topic 2</a>
把它们变成这样:
<ac:link><ri:page ri:content-title="Test_Topic_2" /></ac:link>
我有一个Ant build.xml文件,它可以在上面的链接上工作,但如果路径以../../开头,它就不起作用而不是../
由于获取主题名称的最佳位置是从"Test_topic_2.txt"条目中,我想知道是否有一种方法可以让正则表达式从".txt"向后工作,告诉它匹配从".txt"到遇到的第一个斜杠的所有内容,将其保留,并替换其余部分。
可能有一些完全不同的方法,如果有人有任何想法,请告诉我。
谢谢,
假设文件input.txt
中的输入链接与具有以下内容的构建文件位于同一路径:
<a class="xref" href="../Test_Topic_1/Test_Topic_1.txt">Test Topic 1</a>
<a class="xref" href="../Test_Topic_2/Test_Topic_2.txt">Test Topic 2</a>
<a class="xref" href="../Test_Topic_3/Test_Topic_3.txt">Test Topic 3</a>
<a class="xref" href="../Test_Topic_4/Test_Topic_4.txt">Test Topic 4</a>
您可以将文件加载到一个属性中,然后在每一行上循环并用更新的链接替换它,将更新的链接保存在属性中并将其附加到输出文件中,如下所示。
<loadfile property="file.content" srcFile="./input.txt" />
<for list="${file.content}" param="original.href" delimiter="${line.separator}">
<sequential>
<var name="updated.href" unset="true" />
<propertyregex input="@{original.href}" property="updated.href" regexp="<a class="xref" href=".+/([^/]+).txt">.+</a>"
replace="<ac:link><ri:page ri:content-title="1" /></ac:link>" />
<echo message="${updated.href}${line.separator}" file="output.txt" append="true" />
</sequential>
</for>
这种情况下的输出是:
<ac:link><ri:page ri:content-title="Test_Topic_1" /></ac:link>
<ac:link><ri:page ri:content-title="Test_Topic_2" /></ac:link>
<ac:link><ri:page ri:content-title="Test_Topic_3" /></ac:link>
<ac:link><ri:page ri:content-title="Test_Topic_4" /></ac:link>
<replaceregexp byline="true">
<regexp pattern="<a class=.*?href=".*?([^/]+).txt">.*?</a>"/>
<substitution expression="<ac:link><ri:page ri:content-title="1" /></ac:link>"/>
<fileset dir="${your.directory.containing.txt.files}">
<include name="**/*.txt"/>
</fileset>
</replaceregexp>
这里,replaceregexp
将处理输入文件的每一行,将其与<regex pattern="..."/>
匹配,并将成功匹配的内容替换为<substitution expression="..."/>
这将针对<fileset>
中的每个文件执行。
因此,例如,如果你有以下目录结构:
../ a / b / 1.txt, 2.txt
../ a / b / c / 3.txt
如果将${your.directory.containing.txt.files}
设置为../a/b/
,则文件1.txt, 2.txt and 3.txt
将逐行处理,<replaceregexp>
和将替换每个匹配的表达式。
请参阅此处的演示。