简体   繁体   中英

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. 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]

No need to say I just started with groovy so probably the code has some mistakes. But any help is appreciated

msg.findAll( /\d{6}/ ) returns an array of strings found.

if you want just first element found then use msg.findAll( /\d{6}/ ) [0]

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

if you want all of them delimited with coma msg.findAll( /\d{6}/ ).join(',')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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