首页 文章

无法从Scala Regex匹配中检索组

提问于
浏览
1

我在Scala(2.11.5)中使用regexp挣扎,我有一个跟随字符串来解析(例子):

val string = "http://sth.com/sth/56,57597,14058913,Article_title,,5.html"

我想在上面的字符串中提取第三个数值(它需要在斜杠之后为第三个,因为可能有其他组跟随),为了做到这一点,我有以下正则表达式模式:

val pattern = """\/\d+,\d+,(\d+)""".r

我一直试图检索第三个数字序列的组,但似乎没有什么对我有用 .

val matchList = pattern.findAllMatchIn(string).foreach(println)
val matchListb = pattern.findAllIn(string).foreach(println)

我也试过使用匹配模式 .

string match {
    case pattern(a) => println(a)
    case _ => "What's going on?"
}

并得到了相同的结果 . 要么返回整个正则表达式,要么返回任何内容 .

有一种简单的方法可以在Scala中检索组表单regexp模式吗?

2 回答

  • 2

    您可以使用 scala.util.matching.Regex.Matchgroup 方法来获取结果 .

    val string = "http://sth.com/sth/56,57597,14058913,Article_title,,5.html"
    val pattern = """\/\d+,\d+,(\d+)""".r
    
    val result = pattern.findAllMatchIn(string)  // returns iterator of Match
                        .toArray                 
                        .headOption              // returns None if match fails
                        .map(_.group(1))         // select first regex group
    
    // or simply
    
    val result = pattern.findFirstMatchIn(string).map(_.group(1)) 
    
    // result = Some(14058913)
    // result will be None if the string does not match the pattern.
    
    // if you have more than one groups, for instance:
    // val pattern = """\/(\d+),\d+,(\d+)""".r 
    // result will be Some(56)
    
  • 2

    模式匹配通常是最简单的方法,但它需要匹配整个字符串,因此您必须使用 .* 为正则表达式模式添加前缀和后缀:

    val string = "http://sth.com/sth/56,57597,14058913,Article_title,,5.html"
    val pattern = """.*\/\d+,\d+,(\d+).*""".r
    val pattern(x) = string
    // x: String = 14058913
    

相关问题