Skip to content

Instantly share code, notes, and snippets.

@daneko
Last active December 28, 2015 00:19
Show Gist options
  • Save daneko/7412131 to your computer and use it in GitHub Desktop.
Save daneko/7412131 to your computer and use it in GitHub Desktop.
s"\\$$" ← 今日のメモ

$マークを出力したいだけ

環境:Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).

これは動く

import scala.util.matching.Regex
import Regex._

val s = "hoge:1000\tfuga:2000"
val reg = new Regex(""":([0-9]{4})""", "i")
val f: Match => String = { m =>
  val l = m.group("i").toInt * 1000
  s"piyo:$l"
}
reg.replaceAllIn(s , f).replace("piyo", "$piyo")

これはだめ

import scala.util.matching.Regex
import Regex._

val s = "hoge:1000\tfuga:2000"
val reg = new Regex(""":([0-9]{4})""", "i")
val f: Match => String = { m =>
  val l = m.group("i").toInt * 1000
  s"$$piyo:$l" // s"\\$$piyo:$l" こうする
}
reg.replaceAllIn(s , f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment