Convert String to Map

五迷三道 提交于 2021-01-29 09:50:36

问题


def val = "[name: searchBaseDN value: , name: enable value: false, name: userDn value:  
cn=EAGREAD,ou=Users,ou=Administration,o=hyn, name: base value:  , name: prefsize value: 1,   
name: timeBetweenEvictionRuns value: 300000]"

I would like to convert it into hashmap val[name:value]

Thanks for your help .


回答1:


This looks promising:

matches = val =~ /name:\s+(.*?)\s+value:\s+(.*?)(,\s+|\s+|])/

def newVal = [:]
matches.each { m ->
    newVal[m[1]] = m[2]
}

But if the string is changed slightly, the regex might break...



来源:https://stackoverflow.com/questions/17691304/convert-string-to-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!