Regex 大写,小写,将Ant属性大写
在Ant中,我有一个名为“Regex 大写,小写,将Ant属性大写,regex,ant,Regex,Ant,在Ant中,我有一个名为“some_property”的属性,假设它的值是“hello” 我正在尝试用此属性的值(“hello”)替换文本文件中的占位符,作为大写 因此,我有一个任务: <replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true"> 我希望它能像我有这个任务一样工作: <replaceregexp match="SOME_PLACE_HOLDER" rep
some_property
”的属性,假设它的值是“hello
”
我正在尝试用此属性的值(“hello”)替换文本文件中的占位符,作为大写因此,我有一个任务:
<replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true">
我希望它能像我有这个任务一样工作:
<replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true">
我希望避免外部Ant任务(例如Ant Contrib),因此解决方案需要是纯正则表达式-这必须是可能的
大写、小写和大写
有人知道正确的正则表达式吗?我知道您希望避免使用Ant扩展,但使用正则表达式实现解决方案的限制有点紧-如果以下情况太过偏离(打破?)这一规则,我深表歉意 现在Ant附带了一个javascript引擎,所以在Ant xml中实现的任何看起来有问题的东西通常都可以隐藏在Java中。下面是四种可以改变大小写的方法 在您的情况下,您可以获取
some_属性
属性,并通过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>
多亏了Poni和Marco Demaio的帮助,您可以使用类似于
SCriptdef
的语言和任何方便的语言
<scriptdef language="javascript" name="upper">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toUpperCase() );
</scriptdef>
project.setProperty(attributes.get(“to”),
attributes.get(“string”).toUpperCase();
这里提到JavaScript作为一个例子。您还可以使用任何其他。知道哪些似乎不支持传递参数。不管怎样,有了你的答案和现场的答案,我已经得到了我想要的。所以,非常感谢马丁!要使用,您需要使用Java1.6,或者在Ant的lib
目录中有BSF(BSF需要jakarta commons-logging.jar和Mozilla Rhino js.jar)
[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'
<scriptdef language="javascript" name="upper">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toUpperCase() );
</scriptdef>