Scala:如何公开case语句中绑定的regex变量

Scala:如何公开case语句中绑定的regex变量,regex,scala,case,Regex,Scala,Case,Scala正则表达式在以下两种情况下都非常有效: 无条件执行的代码: e、 g 或者,如果在case语句中,我们使用表达式(并且不再需要它们) 但是,如果我需要在case语句之外“显示”与变量的匹配,该怎么办?具体而言,var如下所示 val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r var category = "" ; var aaUrl = "";var title = "" line match { case li

Scala正则表达式在以下两种情况下都非常有效:
无条件执行的代码: e、 g

或者,如果在case语句中,我们使用表达式(并且不再需要它们)

但是,如果我需要在case语句之外“显示”与变量的匹配,该怎么办?具体而言,var如下所示

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
var category = "" ; var aaUrl = "";var title = ""
line match {
case lineregx(category, aaUrl, title) => val lineregx(category, aaUrl, title) = line
case anotherRegx(category, aaUrl) => val lineregx(category, aaUrl) = line
case _ => { println("did not match line %s".format(line)); return 0 }
}
// Do something with category, aaUrl, title HERE after the case statement.
问题是,应用lineregx/anotherregex的语法使这些变量仅局限于case语句

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
val (category, aaUrl, title) = line match {
  case lineregx(category, aaUrl, title) => (category, aaUrl, title)
  case anotherRegx(category, aaUrl) => (category, aaUrl, ???)
  case _ => { println("did not match line %s".format(line)); return 0 }
}
// Do something with category, aaUrl, title HERE after the case statement.
但这段代码相当混乱。首先,对于第二种情况,存在
title
的价值问题。另一方面,还有提前返回。相反,代码最好是这样组织的:

// method declaration
// ...
  val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
  line match {
    case lineregx(category, aaUrl, title) => f(category, aaUrl, title)
    case anotherRegx(category, aaUrl)     => f(category, aaUrl, ???)
    case _ => 
      println("did not match line %s".format(line))
      0
  }
}  // end of method

def f(category: String, aaUrl: String, title: String): Int = {
  // Do something with category, aaUrl, title HERE
}
大致上

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
val (category, aaUrl, title) = line match {
  case lineregx(category, aaUrl, title) => (category, aaUrl, title)
  case anotherRegx(category, aaUrl) => (category, aaUrl, ???)
  case _ => { println("did not match line %s".format(line)); return 0 }
}
// Do something with category, aaUrl, title HERE after the case statement.
但这段代码相当混乱。首先,对于第二种情况,存在
title
的价值问题。另一方面,还有提前返回。相反,代码最好是这样组织的:

// method declaration
// ...
  val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
  line match {
    case lineregx(category, aaUrl, title) => f(category, aaUrl, title)
    case anotherRegx(category, aaUrl)     => f(category, aaUrl, ???)
    case _ => 
      println("did not match line %s".format(line))
      0
  }
}  // end of method

def f(category: String, aaUrl: String, title: String): Int = {
  // Do something with category, aaUrl, title HERE
}

您可以使用
选项

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r

val (maybeCat, maybeUrl, maybeTitle) = 
line match {
  case lineregx(category, aaUrl, title) => (Some(category), Some(aaUrl), Some(title))
  case anotherRegx(category, aaUrl)     => (Some(category), Some(aaUrl), None)
  case _ => 
    println("did not match line %s".format(line))
    (None, None, None)
}

var category = maybeCat getOrElse ""
var aaUrl =    maybeURL getOrElse ""
var title =    maybeTitle getOrElse  ""

稍微详细一点,但这样您可以在相同的范围内获取变量。

您可以使用
选项

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r

val (maybeCat, maybeUrl, maybeTitle) = 
line match {
  case lineregx(category, aaUrl, title) => (Some(category), Some(aaUrl), Some(title))
  case anotherRegx(category, aaUrl)     => (Some(category), Some(aaUrl), None)
  case _ => 
    println("did not match line %s".format(line))
    (None, None, None)
}

var category = maybeCat getOrElse ""
var aaUrl =    maybeURL getOrElse ""
var title =    maybeTitle getOrElse  ""

稍微详细一点,但这样你可以在同一个范围内获得变量。

感谢这里的反馈,我希望能够在同一个方法中的块中分配类别、aaurl和title变量后直接访问它们,而不是将它们划分为子范围内使用(“f”方法)。建议的解决方案并没有实现这一点:但我将标记为有帮助,因为它使我相信可能没有解决方案来实现我的原始意图。@Daniel C.Sobral:代码中的变量(
类别
aaUrl
&
标题
,第2行)未使用且非常混乱(它们仅被下面定义的VAL遮挡)。可能是OP代码片段的遗留部分,但您可能希望删除它。@RégisJean Gilles说得对,谢谢!我将删除它们。感谢这里的反馈,我希望能够在同一方法中的块中赋值后直接访问category、aaurl和title变量,而不是将它们划分为在子范围内使用(“f”方法)。建议的解决方案无法实现这一点:但我将标记为有用,因为它使我相信可能没有解决方案来实现我的原始意图。@Daniel C.Sobral:代码中的变量(
类别
aaUrl
&
标题
,第2行)未使用且非常混乱(它们仅被下面定义的VAL遮挡)。可能是OP代码片段的遗留部分,但您可能希望删除它。@RégisJean Gilles说得对,谢谢!我将删除它们。顺便说一句,我最终将无可辩驳的匹配器分配给匹配中的临时值,然后将临时VAL分配给外部作用域的VAR。这使我能够按照描述维护流顺便说一句,在OP.btw中,我最终将无可辩驳的匹配器分配给匹配中的临时值,然后将临时VAL分配给外部作用域变量。这使我能够按照OP中的描述保持流。