I understand that you want to avoid Ant extensions, but the constraint that the solution be implemented using regex is a little tight - apologies if the following bends (breaks?) that rule too much.
Ant ships with a javascript engine these days, so anything that seems problematic to implement in Ant xml can usually be hidden away in a scriptdef
. Below are four that do case changing.
In your case, you would take your some_property
property and process it through the upper
script to get an uppercased version of the string to use in the replaceregexp
task.
<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( /^.|sS/g,
function(a) { return a.toUpperCase(); }) );
</scriptdef>
Example use:
<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}'" />
And output:
[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'
Thanks to Poni and Marco Demaio for the implementation of the Capitalization.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…