我有以下格式的String
blah blah [user:1] ho ho [user:2]他是他
我希望它被替换为
blah blah< a href ='1'> someFunctionCall(1)< / a> ho ho< a href ='2'> someFunctionCall(2)< / a>他是他
所以有两件事取代了[user:id]和一个methodCall
注意:我想在groovy中做到这一点,这样做的有效方法是什么
解决方法
Groovy,宝贝:
def someFunctionCall = { "someFunctionCall(${it})" } assert "blah blah [user:1] ho ho [user:2] he he he" .replaceAll(/\[user:(\d+)]/){ all,id -> "<a href=\"${id}\">${someFunctionCall(id)}</a>" } == "blah blah <a href=\"1\">someFunctionCall(1)</a> ho ho <a href=\"2\">someFunctionCall(2)</a> he he he"