How to check whether a String fully matches a Regex in Scala?

后端 未结 6 411
长发绾君心
长发绾君心 2020-12-23 11:14

Assume I have a Regex pattern I want to match many Strings to.

val Digit = \"\"\"\\d\"\"\".r

I just want to check whether a given String fu

6条回答
  •  甜味超标
    2020-12-23 11:47

    Using Standard Scala library and a pre-compiled regex pattern and pattern matching (which is scala state of the art):

    val digit = """(\d)""".r
    
    "2" match {
      case digit( a) => println(a + " is Digit")
      case _ => println("it is something else")
    }
    

    more to read: http://www.scala-lang.org/api/2.12.1/scala/util/matching/index.html

提交回复
热议问题