根据文件中的正则表达式设置 Ant 属性
我的文件中有以下内容
version: [0,1,0]
,我想将 Ant 属性设置为字符串值 0.1.0
。
正则表达式是
version:[[:space:]]\[([[:digit:]]),([[:digit:]]),([[:digit:]])\]
,然后我需要将属性设置为
\1.\2.\3
,以便
0.1.0
我无法弄清楚如何一起使用 Ant 任务来执行此操作。
我有 Ant-contrib,因此可以使用这些任务。
I have the following in a file
version: [0,1,0]
and I would like to set an Ant property to the string value 0.1.0
.
The regular expression is
version:[[:space:]]\[([[:digit:]]),([[:digit:]]),([[:digit:]])\]
and I need to then set the property to
\1.\2.\3
to get
0.1.0
I can't workout how to use the Ant tasks together to do this.
I have Ant-contrib so can use those tasks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于马特的第二个解决方案,这对我来说适用于任何(文本)文件,无论一行与否。它没有 apache-contrib 依赖项。
Based on matt's second solution, this worked for me for any (text) file, one line or not. It has no apache-contrib dependencies.
这是一种不使用 ant-contrib 的方法,使用
loadproperties
和filterchain
(请注意<一href="http://ant.apache.org/manual/Types/filterchain.html#replaceregex" rel="noreferrer">replaceregex
是一个“字符串过滤器” - 请参阅tokenfilter
文档 - 而不是replaceregexp
任务):请注意正则表达式有点不同,我们将该文件视为属性文件。
或者,您可以将
loadfile
与一起使用filterchain
,例如,如果您要加载的文件不是属性格式。例如,如果文件内容只是
[0,1,0]
并且您希望将version
属性设置为0.1.0
,你可以这样做:Here's a way that doesn't use ant-contrib, using
loadproperties
and afilterchain
(note thatreplaceregex
is a "string filter" - see thetokenfilter
docs - and not thereplaceregexp
task):Note the regex is a bit different, we're treating the file as a property file.
Alternatively you could use
loadfile
with afilterchain
, for instance if the file you wanted to load from wasn't in properties format.For example, if the file contents were just
[0,1,0]
and you wanted to set theversion
property to0.1.0
, you could do something like:用这个解决了这个问题:
但这似乎有点浪费 - 它将整个文件加载到一个属性中!
如果有人有更好的建议请留言:)
Solved it with this:
But it seems a little wasteful - it loads the whole file into a property!
If anyone has any better suggestions please post :)