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

后端 未结 6 417
长发绾君心
长发绾君心 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:27

    For the full match you may use unapplySeq. This method tries to match target (whole match) and returns the matches.

    scala> val Digit = """\d""".r
    Digit: scala.util.matching.Regex = \d
    
    scala> Digit unapplySeq "1"
    res9: Option[List[String]] = Some(List())
    
    scala> Digit unapplySeq "123"
    res10: Option[List[String]] = None
    
    scala> Digit unapplySeq "string"
    res11: Option[List[String]] = None
    

提交回复
热议问题