Collections 如何在groovy映射中使用正则表达式
我有一张类似的地图Collections 如何在groovy映射中使用正则表达式,collections,groovy,map,Collections,Groovy,Map,我有一张类似的地图 xxx-10.name ='welcome' xxx-10.age ='12' xxx-10.std ='2nd' xxx-12.name ='welcome' xxx-12.age ='12' xxx-12.std ='2nd' yyy-10.name ='welcome' yyy-10.age ='12' yyy-10.std ='2nd' yyy-12.
xxx-10.name ='welcome'
xxx-10.age ='12'
xxx-10.std ='2nd'
xxx-12.name ='welcome'
xxx-12.age ='12'
xxx-12.std ='2nd'
yyy-10.name ='welcome'
yyy-10.age ='12'
yyy-10.std ='2nd'
yyy-12.name ='welcome'
yyy-12.age ='12'
yyy-12.std ='2nd'
wen user给xxx我必须返回包含所有xxx条目的子映射,无论与之关联的数字是多少。有没有办法用正则表达式实现这一点?或者在没有对键进行迭代的情况下
SubMap我可以使用该实用程序获取..这应该可以满足您的需要
def fileContents = '''xxx-10.name ='welcome'
|xxx-10.age ='12'
|xxx-10.std ='2nd'
|xxx-12.name ='welcome'
|xxx-12.age ='12'
|xxx-12.std ='2nd'
|yyy-10.name ='welcome'
|yyy-10.age ='12'
|yyy-10.std ='2nd'
|yyy-12.name ='welcome'
|yyy-12.age ='12'
|yyy-12.std ='2nd'''.stripMargin()
// Get a Reader for the String (this could be a File.withReader)
Map map = new StringReader( fileContents ).with {
// Create a new Properties object
new Properties().with { p ->
// Load the properties from the reader
load( it )
// Then for each name, inject into a map
propertyNames().collectEntries {
// Strip quotes off the values
[ (it): p[ it ][ 1..-2 ] ]
}
}
}
findByPrefix = { pref ->
map.findAll { k, v ->
k.startsWith( pref )
}
}
findByPrefix( 'xxx' )
祈祷你不要删除这个问题- 这应该是你想要的
def fileContents = '''xxx-10.name ='welcome'
|xxx-10.age ='12'
|xxx-10.std ='2nd'
|xxx-12.name ='welcome'
|xxx-12.age ='12'
|xxx-12.std ='2nd'
|yyy-10.name ='welcome'
|yyy-10.age ='12'
|yyy-10.std ='2nd'
|yyy-12.name ='welcome'
|yyy-12.age ='12'
|yyy-12.std ='2nd'''.stripMargin()
// Get a Reader for the String (this could be a File.withReader)
Map map = new StringReader( fileContents ).with {
// Create a new Properties object
new Properties().with { p ->
// Load the properties from the reader
load( it )
// Then for each name, inject into a map
propertyNames().collectEntries {
// Strip quotes off the values
[ (it): p[ it ][ 1..-2 ] ]
}
}
}
findByPrefix = { pref ->
map.findAll { k, v ->
k.startsWith( pref )
}
}
findByPrefix( 'xxx' )
祈祷你不要删除这个问题- groovy中有一个用于集合的过滤函数。看 在您的情况下,使用以下命令搜索搜索字符串:
groovy中有一个用于集合的过滤函数。看 在您的情况下,使用以下命令搜索搜索字符串: 更好地使用startsWith,因为这是他感兴趣的东西,就像我一样。更好地使用startsWith,因为这是他感兴趣的东西,就像我一样
map.findAll { it.key.startsWith(yourSearchString) }