Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: extracting part of a Strings using Regular Expressions

Tags:

regex

scala

I have a very simple string like this one:

"Some(1234)"

I'd like to extract "1234" out from it. How can I do it?

like image 581
Max Avatar asked Dec 07 '13 20:12

Max


1 Answers

val s = "Some(1234)"
//s: java.lang.String = Some(1234)

val Pattern = """Some\((\d+)\)""".r
//Pattern: scala.util.matching.Regex = Some\((\d+)\)

val Pattern(number) = s
//number: String = 1234

Switch out your regex for whatever you need. \d+ limits it to digits only.

like image 181
Akos Krivachy Avatar answered Oct 14 '22 21:10

Akos Krivachy