我使用Scalate进行模板制作。通常是这样的:
模板:
Hello {{name}}
参数:
`Map(“ name”->“ Peter”)
结果:
Hello Peter
是否可以获取参数图作为结果?
模板:
Hello {{name}}
请求:
Hello Peter
结果:
Map("name" -> "Peter")
答案 0 :(得分:2)
也许您正在寻找带有命名组的正则表达式?
//Regex with named groups
val pattern = """^Hello (?<firstname>\w+) (?<lastname>\w+)$""".r
val groups = List(
"firstname",
"lastname"
)
def matchAll(str: String): Option[Map[String, String]] = pattern
.findFirstMatchIn(str)
.map { matched =>
groups.map(name => name -> matched.group(name)).toMap
}
matchAll("Hello Joe Doe") //Some(Map(firstname -> Joe, lastname -> Doe))