2011-08-20 251 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爲例。你也可以使用其他的。