代替带/在groovy中
我知道这是一个非常具体的问题,因为我自己没有做替换,我需要在groovy中替换这个字符串: com.pantest中的com/pantest 我试过这个:代替带/在groovy中,groovy,Groovy,我知道这是一个非常具体的问题,因为我自己没有做替换,我需要在groovy中替换这个字符串: com.pantest中的com/pantest 我试过这个: groupId =com.pantest def mavenGroupID = groupId.replaceAll('.','/') 这是我在输出中得到的: echo mavenGroupID is ////////// mavenGroupID is ////////// 点(.)是某种特殊的字符吗?我试图用****来转义它,但它也不
groupId =com.pantest
def mavenGroupID = groupId.replaceAll('.','/')
这是我在输出中得到的:
echo mavenGroupID is //////////
mavenGroupID is //////////
点(.)是某种特殊的字符吗?我试图用****来转义它,但它也不起作用。如评论中所述,使用正则表达式作为输入,因此这意味着至少需要转义点,但实际上,还必须转义字符-反斜杠
\
(更多线索请访问)
因此,您可以按如下方式操作:
def test = "aaa.bbb.ccc"
//want to replace ., need to use escape char \, but it needs to be escaped as well , so \\
println test.replaceAll('\\.','/')
输出符合要求aaa/bbb/ccc
replaceAll(“\\.”,“/”)
是键
正如注释中提到的,使用正则表达式作为输入,这意味着至少需要转义点,但实际上,还必须转义字符-反斜杠
\
(更多线索请访问)
因此,您可以按如下方式操作:
def test = "aaa.bbb.ccc"
//want to replace ., need to use escape char \, but it needs to be escaped as well , so \\
println test.replaceAll('\\.','/')
输出符合要求aaa/bbb/ccc
replaceAll(“\\.”,“/”)
是键
public-String-replaceAll(String-regex,String-replacement){
,尝试\.
基于的应按建议使用`so
\.`进行转义,或通过[.]
将其放入匹配组(replaceAll将regex作为输入)public-String-replaceAll(String-regex,String-replacement){
,请尝试\。
基于应按建议使用`so
\ `转义,或通过[.]
将其放入匹配组(replaceAll将regex作为输入)