大写字母,小写字母,大写一个Ant属性 [英] UPPERCASE, lowercase, Capitalize an Ant property

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

问题描述

在蚂蚁,我有一个名为 some_property 属性,并假设其值为你好

In Ant, I have a property named 'some_property', and let's say its value is "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任务的(如蚂蚁的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?

推荐答案

据我了解,你想避免蚂蚁扩展,但该解决方案使用正则表达式是有点紧实施约束 - 道歉,如果下弯(休息? )该规则太多了。

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.

蚂蚁附带一个JavaScript引擎,这些天,所以任何事情都有可能通常在的 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 属性和处理它通过脚本得到一个大写的字符串的版本在 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'

感谢波尼和Marco Demaio的<一个href=\"http://stackoverflow.com/questions/2332811/capitalize-words-in-string/5678673#5678673\">implementation资本化的。

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

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