2011-08-20 35 views
19

Antでは、私は 'some_property'という名前のプロパティを持っていて、その値は「hello」としましょう。大文字、小文字、Antプロパティを大文字にする

私は大文字としてこのプロパティの値を持つテキストファイル(「ハロー」)内のプレースホルダを置き換えるにしようとしています。
だから、私はこの仕事があります。私は、このような(外部のAntタスクにを避けたい

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

:私はこのタスクを持っているかのよう

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

をそして、私はそれがを仕事をしたいですAnt-Contribとして)、したがって、ソリューションは、純粋な正規表現である必要があります - 可能でなければなりません!

大文字、小文字、および大文字。

誰でも正しいregexesを知っていますか?

答えて

34

あなたはAntの拡張を避けたいと思っていますが、regexを使ってソリューションを実装するという制約は少し厳しいです。あまりにも多くのルールが崩れると謝罪します。

Antは最近、javascriptエンジンとともに出荷されています。したがって、Ant xmlに実装するのに問題があるようなものは、通常scriptdefに隠すことができます。以下は、ケースを変更する4つです。

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}'" /> 

そして出力:implementation of the CapitalizationためポニとマルコDemaioへ

[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' 

感謝。

+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を例に挙げています。他のものを使うこともできます。