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

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • 1
    is that an array or a String? If it is a String as the code indicates, then you will need to parse and extract that information – aldrin Jul 17 '13 at 05:52

1 Answers1

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

matcauthon
  • 2,261
  • 1
  • 24
  • 41
  • You could use inject as well: `def newVal = matches.inject( [:] ) { m, match -> m << [ (match[1]): match[2] ] }` – tim_yates Jul 17 '13 at 15:14
  • if the val = "[ name: urls value: lp://23.165.3.215:383, lp://23.165.3.216:383, name: searchBaseDN value: ,] what change I need to do in the pattern . In the result only one url is showing .[urls lp://23.165.3.215:383] – Sharing is Good Jul 17 '13 at 17:19
  • @tim_yates or even `def newVal = matches.collect { it[1, 2] }.collectEntries()` =D – epidemian Jul 18 '13 at 00:05