根据文件中的正则表达式设置 Ant 属性

发布于 2024-12-11 11:54:11 字数 383 浏览 1 评论 0原文

我的文件中有以下内容

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-12-18 11:54:11

基于马特的第二个解决方案,这对我来说适用于任何(文本)文件,无论一行与否。它没有 apache-contrib 依赖项。

<loadfile property="version" srcfile="version.txt">
  <filterchain>
    <linecontainsregexp>
      <regexp pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"/>
    </linecontainsregexp>
    <replaceregex pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
  </filterchain>
</loadfile>  

Based on matt's second solution, this worked for me for any (text) file, one line or not. It has no apache-contrib dependencies.

<loadfile property="version" srcfile="version.txt">
  <filterchain>
    <linecontainsregexp>
      <regexp pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"/>
    </linecontainsregexp>
    <replaceregex pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
  </filterchain>
</loadfile>  
说好的呢 2024-12-18 11:54:11

这是一种不使用 ant-contrib 的方法,使用 loadproperties filterchain (请注意<一href="http://ant.apache.org/manual/Types/filterchain.html#replaceregex" rel="noreferrer">replaceregex 是一个“字符串过滤器” - 请参阅tokenfilter 文档 - 而不是 replaceregexp 任务):

<loadproperties srcFile="version.txt">
  <filterchain>
    <replaceregex pattern="\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
  </filterchain>
</loadproperties>

请注意正则表达式有点不同,我们将该文件视为属性文件。

或者,您可以将 loadfile一起使用filterchain,例如,如果您要加载的文件不是属性格式。

例如,如果文件内容只是 [0,1,0] 并且您希望将 version 属性设置为 0.1.0,你可以这样做:

<loadfile srcFile="version.txt" property="version">
  <filterchain>
    <replaceregex pattern="\s+\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
  </filterchain>
</loadfile>

Here's a way that doesn't use ant-contrib, using loadproperties and a filterchain (note that replaceregex is a "string filter" - see the tokenfilter docs - and not the replaceregexp task):

<loadproperties srcFile="version.txt">
  <filterchain>
    <replaceregex pattern="\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
  </filterchain>
</loadproperties>

Note the regex is a bit different, we're treating the file as a property file.

Alternatively you could use loadfile with a filterchain, 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 the version property to 0.1.0, you could do something like:

<loadfile srcFile="version.txt" property="version">
  <filterchain>
    <replaceregex pattern="\s+\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
  </filterchain>
</loadfile>
烟花易冷人易散 2024-12-18 11:54:11

用这个解决了这个问题:

<loadfile property="burning-boots-js-lib-build.lib-version" srcfile="burning-boots.js"/>
<propertyregex property="burning-boots-js-lib-build.lib-version"
    override="true"
    input="${burning-boots-js-lib-build.lib-version}"
    regexp="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"
    select="\1.\2.\3" />

但这似乎有点浪费 - 它将整个文件加载到一个属性中!

如果有人有更好的建议请留言:)

Solved it with this:

<loadfile property="burning-boots-js-lib-build.lib-version" srcfile="burning-boots.js"/>
<propertyregex property="burning-boots-js-lib-build.lib-version"
    override="true"
    input="${burning-boots-js-lib-build.lib-version}"
    regexp="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"
    select="\1.\2.\3" />

But it seems a little wasteful - it loads the whole file into a property!

If anyone has any better suggestions please post :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文