简体   繁体   中英

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

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))

You may replace with 0+ whitespaces ( \\s*+ ) that are preceded with letters and followed with digits:

var str = "Hello123"
val res = str.split("(?<=[a-zA-Z])\\s*+(?=\\d)")
println(res.deep.mkString(", "))  // => Hello, 123

See the online Scala demo

Pattern details :

  • (?<=[a-zA-Z]) - a positive lookbehind that only checks (but does not consume the matched text) if there is an ASCII letter before the current position in the string
  • \\\\s*+ - matches (consumes) zero or more spaces possessively, ie
  • (?=\\\\d) - this check is performed only once after the whitespaces - if any - were matched, and it requires a digit to appear right after the current position in the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM