简体   繁体   English

如何从 groovy 中的字符串替换方括号?

[英]how to replace square brackets from a string in groovy?

I have a string from which I get just the number with 6 charactes using findAll() but tha returns me the number in this format [111111] I want to get rid of the square brackets and save it to a variable.我有一个字符串,我使用findAll()从中只得到 6 个字符的数字,但我以这种格式返回数字[111111]我想去掉方括号并将其保存到变量中。 I have tried this but it does not remove the brackers:我已经尝试过了,但它没有删除括号:

def msg = "Fix for bug 861768 and 3v3 player number 24"
def bugNumber = msg.findAll( /\d{6}/ ).toString()
bugNumber.replaceAll("\\[", "").replaceAll("\\]","");
println bugNumber

This is the output I get: [861768]这是我得到的 output: [861768]

No need to say I just started with groovy so probably the code has some mistakes.不用说我刚开始使用 groovy 所以可能代码有一些错误。 But any help is appreciated但任何帮助表示赞赏

msg.findAll( /\d{6}/ ) returns an array of strings found. msg.findAll( /\d{6}/ )返回找到的字符串数组。

if you want just first element found then use msg.findAll( /\d{6}/ ) [0]如果您只想找到第一个元素,请使用msg.findAll( /\d{6}/ ) [0]

or msg.find( /\d{6}/ )msg.find( /\d{6}/ )

if you want all of them delimited with coma msg.findAll( /\d{6}/ ).join(',')如果您希望所有这些都用逗号分隔msg.findAll( /\d{6}/ ).join(',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM