尝试模式是这样的:
val pattern = "(\\[\\w+,\\d+\\])=(\\d+)(,\\s)?"r
例子:
scala> val input = """[test,2]=11, [test,3]=12, [test,7]=11, [test,11]=4, [test,16]=5"""
input: String = [test,2]=11, [test,3]=12, [test,7]=11, [test,11]=4, [test,16]=5
scala> val pattern = "(\\[\\w+,\\d+\\])=(\\d+)(,\\s)?"r
warning: there were 1 feature warning(s); re-run with -feature for details
pattern: scala.util.matching.Regex = (\[\w+,\d+\])=(\d+)(,\s)?
scala> val m = pattern.findAllMatchIn(input).map(m => (m.group(1).trim(), m.group(2).trim())).toMap
m: scala.collection.immutable.Map[String,String] = Map([test,2] -> 11, [test,16] -> 5, [test,11] -> 4, [test,3] -> 12, [test,7] -> 11)
scala> m foreach println
([test,2],11)
([test,16],5)
([test,11],4)
([test,3],12)
([test,7],11)
你能指定输出'Map'的类型?第一组和第二组的预期值是多少? –
嗨谢尔盖,从Nyavro下面的答案是我正在寻找。不过谢谢 – codehammer