Skip to content

Instantly share code, notes, and snippets.

@YuukiToriyama
Created August 28, 2019 08:07
Show Gist options
  • Save YuukiToriyama/367b8d33ad4ea230631addca3113b7ef to your computer and use it in GitHub Desktop.
Save YuukiToriyama/367b8d33ad4ea230631addca3113b7ef to your computer and use it in GitHub Desktop.
Scala 正規表現を使って文字列を切り出したい

部分文字列を取り出したい

こんなことがしたい。

echo "aiueo700" | sed 's/\([a-z]*\)\([0-9]*\)/\1 and \2/g'
# => aiueo and 700

正規表現オブジェクトのつくりかた

val regExp = new scala.util.matching.Regex("")

正規表現を使った文字列の取り出し

val id = "aiueo700"
val regExp = new scala.util.matching.Regex("([a-z]*)([0-9]*)")

val result = regExp.findFirstMatchIn(id)

println(result.get.group(0)) 
// => aiueo700
println(result.get.group(1))
// => aiueo
println(result.get.group(2))
// => 700
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment