2011-08-20 16 views
19

In Ant, ho una proprietà denominata "some_property" e diciamo che il suo valore è "hello".MAIUSCOLE, lettere minuscole, maiuscole in una proprietà Ant

Sto cercando di sostituire un segnaposto all'interno di un file di testo con il valore di questa proprietà ("ciao ") come maiuscolo.
Così, ho questo compito:

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

e lo voglio lavorare come se avrei questo compito:

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

desidero evitare attività Ant esterni (come ad come Ant-Contrib), quindi la soluzione deve essere una regex pura - deve essere possibile!

MAIUSCOLO, lettere minuscole e lettere maiuscole.

Qualcuno conosce le regex corrette?

risposta

34

Capisco che si vogliono evitare le estensioni Ant, ma il vincolo che la soluzione sia implementata usando regex è un po 'stretto - scuse se le seguenti curve (interruzioni?) Che governano troppo.

Ant navi con un motore javascript in questi giorni, quindi tutto ciò che sembra problematico da implementare in Ant xml di solito può essere nascosto in un scriptdef. Di seguito sono quattro che cambiano caso.

Nel tuo caso, si dovrebbe prendere la vostra proprietà some_property ed elaborare attraverso lo script upper per ottenere una versione maiuscolo della stringa da utilizzare nel compito 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> 

Esempio utilizzo:

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

E uscita:

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

Grazie a Poni e Marco Demaio per la implementation of the Capitalization.

+0

Conosciuto su

0

È possibile utilizzare qualcosa di simile a SCriptdef e qualsiasi altra lingua utile.

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

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

Qui JavaScript è menzionato come esempio. Puoi anche usare qualsiasi altro.

Problemi correlati