gpt4 book ai didi

regex - 正则表达式和字符串操作

转载 作者:行者123 更新时间:2023-12-01 01:21:10 25 4
gpt4 key购买 nike

我有以下格式的字符串
blah blah [user:1] ho ho [user:2] he he he
我希望它被替换
blah blah <a href='1'>someFunctionCall(1)</a> ho ho <a href='2'>someFunctionCall(2)</a> he he he
所以有两件事替换了 [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"

关于regex - 正则表达式和字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9144597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com