1

I use Scalate for templating. Usually it goes this way:

  • Template:

    Hello {{name}}

  • Parameters:

    `Map("name" -> "Peter")

  • Result:

    Hello Peter

Is there a way to get the Parameter Map as Result?

  • Template:

    Hello {{name}}

  • Request:

    Hello Peter

  • Result:

    Map("name" -> "Peter")

Community
  • 1
  • 1
pme
  • 14,156
  • 3
  • 52
  • 95
  • I don't think so. I also don't think your problem has a unique solution. Assume the template is `Hello {{name}} {{surname}}` and you've got `Hello Abc Def Xyz`. How do you know where the `name` ends and the `surname` starts? – SergGr Jan 25 '19 at 22:48
  • I need it for an URL. So I think if you limit the parameter for just one path element, it will be possible - but you are right if you don't define some characters that are not allowed as parameters it's not possible. – pme Jan 26 '19 at 06:28

1 Answers1

2

Maybe you're looking for regex with named groups?

//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))
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76