怎样在scala正则表达式提取器中使用小括号
展开全部
def unapplySeq(target: Any): Option[List[String]] = target match {
case s: CharSequence =>
val m = pattern matcher s
if (runMatcher(m)) Some((1 to m.groupCount).toList map m.group)
else None
case m: Match => unapplySeq(m.matched)
case _ => None
}
protected def runMatcher(m: Matcher) = m.matches()
m.group(1 to groupCount) 是全取出来了,所以小括号套小括号也没问题:
scala> val SP = "((\\d*) (\\w*))".r
SP: scala.util.matching.Regex = ((\d*) (\w*))
scala> val SP(all, a, b) = "123 abc"
all: String = 123 abc
a: String = 123
b: String = abc
case s: CharSequence =>
val m = pattern matcher s
if (runMatcher(m)) Some((1 to m.groupCount).toList map m.group)
else None
case m: Match => unapplySeq(m.matched)
case _ => None
}
protected def runMatcher(m: Matcher) = m.matches()
m.group(1 to groupCount) 是全取出来了,所以小括号套小括号也没问题:
scala> val SP = "((\\d*) (\\w*))".r
SP: scala.util.matching.Regex = ((\d*) (\w*))
scala> val SP(all, a, b) = "123 abc"
all: String = 123 abc
a: String = 123
b: String = abc
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询