user6893681 user6893681 - 2 months ago 9
Scala Question

Scala: string pattern matching and splitting

I am new to Scala and want to create a function to split

Hello123
or
Hello 123
into two strings as follows:

val string1 = 123
val string2 = Hello


What is the best way to do it, I have attempted to use regex matching
\\d
and
\\D
but I am not sure how to write the function fully.

Regards

Answer

Based on the given string I assume you have to match a string and a number with any number of spaces in between

here is the regex for that

([a-zA-Z]+)\\s*(\\d+)

Now create a regex object using .r

"([a-zA-Z]+)\\s*(\\d+)".r

Scala REPL

scala> val regex = "([a-zA-Z]+)\\s*(\\d+)".r

scala> val regex(a, b) = "hello 123"
a: String = "hello"
b: String = "123"

scala> val regex(a, b) = "hello123"
a: String = "hello"
b: String = "123"

Function to handle pattern matching safely

pattern match with extractors

str match {
  case regex(a, b) => Some(a -> b.toInt) 
  case _ => None 
}

Here is the function which does Regex with Pattern matching

def matchStr(str: String): Option[(String, Int)] = {
  val regex = "([a-zA-Z]+)\\s*(\\d+)".r
  str match { 
    case regex(a, b) => Some(a -> b.toInt) 
    case _ => None 
  }
}

Scala REPL

scala> def matchStr(str: String): Option[(String, Int)] = {
    val regex = "([a-zA-Z]+)\\s*(\\d+)".r
    str match {
      case regex(a, b) => Some(a -> b.toInt)
      case _ => None
    }
  }
defined function matchStr

scala> matchStr("Hello123")
res41: Option[(String, Int)] = Some(("Hello", 123))

scala> matchStr("Hello 123")
res42: Option[(String, Int)] = Some(("Hello", 123))
Comments