Scala capture group using regex

后端 未结 5 912
[愿得一人]
[愿得一人] 2020-12-12 23:42

Let\'s say I have this code:

val string = \"one493two483three\"
val pattern = \"\"\"two(\\d+)three\"\"\".r
pattern.findAllIn(string).foreach(println)
         


        
5条回答
  •  一生所求
    2020-12-13 00:02

    def extractFileNameFromHttpFilePathExpression(expr: String) = {
    //define regex
    val regex = "http4.*\\/(\\w+.(xlsx|xls|zip))$".r
    // findFirstMatchIn/findAllMatchIn returns Option[Match] and Match has methods to access capture groups.
    regex.findFirstMatchIn(expr) match {
      case Some(i) => i.group(1)
      case None => "regex_error"
    }
    }
    extractFileNameFromHttpFilePathExpression(
        "http4://testing.bbmkl.com/document/sth1234.zip")
    

提交回复
热议问题