Scala 中的正则表达式和模式匹配第二部分
作为这个问题的后续内容
这里是一些编译和运行的代码正确地,使用捕获。
val myString = "ACATCGTAGCTGCTAGCTG"
val nucCap = "([ACTG]+)".r
myString match {
case nucCap(myNuc) => println("dna:"+myNuc)
case _ => println("not dna")
}
>scala scalaTest.scala
dna:ACATCGTAGCTGCTAGCTG
这是更简单的代码,没有捕获,无法编译。
val myString = "ACATCGTAGCTGCTAGCTG"
val nuc = "[ACGT]+".r
myString match {
case nuc => println("dna")
case _ => println("not dna")
}
>scala scalaTest.scala
scalaTest.scala:7: error: unreachable code
似乎无论是否使用捕获,匹配都应该返回布尔值。 这是怎么回事?
As a follow-up to this question
Here is some code that compiles and runs correctly, using captures.
val myString = "ACATCGTAGCTGCTAGCTG"
val nucCap = "([ACTG]+)".r
myString match {
case nucCap(myNuc) => println("dna:"+myNuc)
case _ => println("not dna")
}
>scala scalaTest.scala
dna:ACATCGTAGCTGCTAGCTG
Here is simpler code, without capture, that does not compile.
val myString = "ACATCGTAGCTGCTAGCTG"
val nuc = "[ACGT]+".r
myString match {
case nuc => println("dna")
case _ => println("not dna")
}
>scala scalaTest.scala
scalaTest.scala:7: error: unreachable code
Seems like the matching should return a boolean regardless of whether a capture is used.
What is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
match
块中,nuc
是一个模式变量,并不引用封闭范围内的nuc
。这使得默认情况无法访问,因为简单模式nuc
将匹配任何内容。nuc
上的一对空括号将使语法糖起作用并调用正则表达式上的unapplySeq
方法:避免此陷阱的一种方法是重命名
nuc< /code> 到
Nuc
。以大写字母开头使其成为稳定的标识符,因此它引用封闭范围内的 Nuc,而不是被编译器视为模式变量。上面将打印
"not dna"
,因为这里我们只是将Nuc
与myString
进行比较,并且它们不相等。这是一个错误,但也许是一个不那么令人困惑的错误!在这种情况下,添加括号也会达到预期的效果:
顺便说一下,返回的不是布尔值,而是 Option[List[String]]:
In your
match
block,nuc
is a pattern variable and does not refer to thenuc
in the enclosing scope. This makes the default case unreachable because the simple patternnuc
will match anything.An empty pair of parentheses on
nuc
will make the syntactic sugar work and call theunapplySeq
method on the Regex:One way to avoid this pitfall is to rename
nuc
toNuc
. Starting with an uppercase letter makes it a stable identifier, so that it refers to theNuc
in the enclosing scope, rather than being treated by the compiler as a pattern variable.The above will print
"not dna"
, because here we are simply comparingNuc
tomyString
, and they are not equal. It's a bug, but maybe a less confusing one!Adding the parentheses will have the desired effect in this case too:
By the way, it is not a boolean that is being returned, but an
Option[List[String]]
: