How to extract a substring using regex

前端 未结 14 1526
暖寄归人
暖寄归人 2020-11-22 13:37

I have a string that has two single quotes in it, the \' character. In between the single quotes is the data I want.

How can I write a regex to extract

14条回答
  •  感动是毒
    2020-11-22 14:26

    In Scala,

    val ticks = "'([^']*)'".r
    
    ticks findFirstIn mydata match {
        case Some(ticks(inside)) => println(inside)
        case _ => println("nothing")
    }
    
    for (ticks(inside) <- ticks findAllIn mydata) println(inside) // multiple matches
    
    val Some(ticks(inside)) = ticks findFirstIn mydata // may throw exception
    
    val ticks = ".*'([^']*)'.*".r    
    val ticks(inside) = mydata // safe, shorter, only gets the first set of ticks
    

提交回复
热议问题