假设我有以下代码:
val string = "one493two483three"
val pattern = """two(\d+)three""".r
pattern.findAllIn(string).foreach(println)
我原本期望findAllIn
只返回483
,但实际却返回了two483three
。我知道可以使用unapply
来提取想要的部分,但需要针对整个字符串设置一个模式,类似于:
val pattern = """one.*two(\d+)three""".r
val pattern(aMatch) = string
println(aMatch) // prints 483
还有其他的方法可以实现这一点,不直接使用 Java.util 中的类,也不使用 unapply 吗?
pattern.findAllMatchIn(string).foreach...
来代替。 - ruhong