The Artima Developer Community
Sponsored Link

Scala Buzz
String interpolation in Scala with Regex

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Sobral

Posts: 80
Nickname: dcsobral
Registered: Aug, 2008

Daniel Sobral is an old dog trying to learn new tricks.
String interpolation in Scala with Regex Posted: Jan 25, 2010 8:35 PM
Reply to this message Reply

This post originated from an RSS feed registered with Scala Buzz by Daniel Sobral.
Original Post: String interpolation in Scala with Regex
Feed Title: Algorithmically challenged
Feed URL: http://dcsobral.blogspot.com/feeds/posts/default
Feed Description: Random thoughts of an IT worker in the stone age of computer science.
Latest Scala Buzz Posts
Latest Scala Buzz Posts by Daniel Sobral
Latest Posts From Algorithmically challenged

Advertisement
Some new stuff I was waiting for has arrived on Scala. We can now have a limited form of string interpolation very easily:


def interpolate(text: String, vars: Map[String, String]) =
"""\$\{([^}]+)\}""".r.replaceAllMatchDataIn(text, {
case Regex.Groups(v) => vars.getOrElse(v, "")
})


Which can then be used like this:


scala> "The ${name} of the ${game}"
res0: java.lang.String = The ${name} of the ${game}

scala> Map("name" -> "X", "game" -> "Y")
res1: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(name -> X, game -> Y)

scala> interpolate(res0, res1)
res2: String = The X of the Y

Read: String interpolation in Scala with Regex

Topic: Scala 2.8.0 beta 1 released Previous Topic   Next Topic Topic: Progress of Migrating AIOTrade to Scala #2

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use