大写、小写、大写 Ant 属性 [英] UPPERCASE, lowercase, Capitalize an Ant property

查看:40
本文介绍了大写、小写、大写 Ant 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ant 中,我有一个名为some_property"的属性,假设它的值为hello".

In Ant, I have a property named 'some_property', and let's say its value is "hello".

我正在尝试用这个属性的值(hello")作为大写替换文本文件中的占位符强>.
所以,我有这个任务:

I'm trying to replace a place-holder inside a text file with this property's value ("hello") as an upper-case.
So, I have this task:

<replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true">

而且我希望它像我有这个任务一样工作:

<replaceregexp match="SOME_PLACE_HOLDER" replace="HELLO" byline="true">

我希望避免外部 Ant 任务(例如 Ant-Contrib),因此解决方案需要是一个纯正则表达式 - 它一定有可能!

I wish to avoid external Ant tasks (such as Ant-Contrib), therefore the solution needs to be a pure regex - it must be possible!

大写、小写和大写.

有人知道正确的正则表达式吗?

Anyone knows the correct regexes?

推荐答案

我知道您想避免使用 Ant 扩展,但是使用正则表达式实现的解决方案的约束有点严格 - 如果以下弯曲(中断?) 这个规则太多了.

I understand that you want to avoid Ant extensions, but the constraint that the solution be implemented using regex is a little tight - apologies if the following bends (breaks?) that rule too much.

现在 Ant 附带了一个 javascript 引擎,因此任何在 Ant xml 中实现似乎有问题的东西通常都可以隐藏在 scriptdef.以下是四个进行大小写更改的内容.

Ant ships with a javascript engine these days, so anything that seems problematic to implement in Ant xml can usually be hidden away in a scriptdef. Below are four that do case changing.

在您的情况下,您将获取您的 some_property 属性并通过 upper 脚本对其进行处理,以获得要在 replaceregexp 中使用的字符串的大写版本 任务.

In your case, you would take your some_property property and process it through the upper script to get an uppercased version of the string to use in the replaceregexp task.

<scriptdef language="javascript" name="upper">
    <attribute name="string" /> 
    <attribute name="to" />

    project.setProperty( attributes.get( "to" ),
                         attributes.get( "string" ).toUpperCase() );
</scriptdef>

<scriptdef language="javascript" name="lower">
    <attribute name="string" /> 
    <attribute name="to" />

    project.setProperty( attributes.get( "to" ),
                         attributes.get( "string" ).toLowerCase() );
</scriptdef>

<scriptdef language="javascript" name="ucfirst">
    <attribute name="string" /> 
    <attribute name="to" />

    var the_string = attributes.get( "string" );
    project.setProperty( attributes.get( "to" ),
                the_string.substr(0,1).toUpperCase() + the_string.substr(1) );
</scriptdef>

<scriptdef language="javascript" name="capitalize">
    <attribute name="string" />
    <attribute name="to" />

    var s = new String( attributes.get( "string" ) );
    project.setProperty( attributes.get( "to" ),
            s.toLowerCase().replace( /^.|\s\S/g,
            function(a) { return a.toUpperCase(); }) );
</scriptdef>

示例使用:

<property name="phrase" value="the quick brown FOX jUmped oVer the laZy DOG" />

<upper string="${phrase}" to="upper" />
<lower string="${phrase}" to="lower" />
<ucfirst string="${phrase}" to="ucfirst" />
<capitalize string="${phrase}" to="capitalize" />

<echo message="upper( ${phrase} )${line.separator}= '${upper}'" />
<echo message="lower( ${phrase} )${line.separator}= '${lower}'" />
<echo message="ucfirst( ${phrase} )${line.separator}= '${ucfirst}'" />
<echo message="capitalize( ${phrase} )${line.separator}= '${capitalize}'" />

和输出:

[echo] upper( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'
[echo] lower( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'the quick brown fox jumped over the lazy dog'
[echo] ucfirst( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The quick brown FOX jUmped oVer the laZy DOG'
[echo] capitalize( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The Quick Brown Fox Jumped Over The Lazy Dog'

感谢 Poni 和 Marco Demaio 实现大写a>.

这篇关于大写、小写、大写 Ant 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆