2011-08-20 176 views
19

在蚂蚁,我有一个名为“some_property”属性,并假设其值为“hello”。大写字母,小写字母,大写的Ant属性

我想与这个属性的值(“你好”)作为大写替换文本文件内的占位符。
所以,我有这样的任务:

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

而且我希望它的工作,如果我将这个任务

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

我想避免外部Ant任务(如如蚂蚁的Contrib),因此解决方案需要一个纯粹的正则表达式 - 它必须是可能的!

大写,小写和大写。

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

回答

34

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

,似乎有问题的蚂蚁XML来实现蚂蚁附带一个JavaScript引擎,这些天,所以任何事情都有可能通常在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' 

由于波尼和Marco Demaio为implementation of the Capitalization

+0

知道约

0

您可以使用类似于SCriptdef任何方便的语言的东西。

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

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

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