大写,小写,大写Ant属性

时间:2011-08-20 04:32:24

标签: regex ant

在Ant中,我有一个名为“some_property”的属性,让我们说它的值为“hello”。

我正在尝试将文本文件中的占位符替换为此属性的值(“ hello ”)作为大写
所以,我有这个任务:

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

我希望它能像一样工作

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

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

大写,小写和大写。

任何人都知道正确的正则表达式吗?

2 个答案:

答案 0 :(得分:34)

我知道您希望避免使用Ant扩展,但使用正则表达式实现解决方案的约束有点紧张 - 如果以下弯曲(中断?)规则太多,则道歉。

Ant最近附带了一个javascript引擎,所以在Ant xml中实现的任何问题通常都可以隐藏在scriptdef中。以下是改变案例的四个。

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

<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的implementation of the Capitalization

答案 1 :(得分:0)

您可以使用与SCriptdef类似的内容和任何方便的语言。

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

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

这里以JavaScript为例。你也可以使用任何其他的。