scala中的正则与其他语言中的正则的用法有些不同,大致有两种用法:
一、匹配字符串中所有符合特定格式的串,比如匹配所有整数:
scala> val numpattern ="""\d+""".r
numpattern: scala.util.matching.Regex = \d+
scala> numpattern.findAllIn("99 a,98 b").toArray
res32: Array[String] = Array(99, 98)
二、匹配特定起始标记与特定结束标记之间的子串,正则式子要重头写到尾,在想要的部分上加上括号:
scala> val ss="Cat Burton##http//.com/catburton?ffff"
ss: String = Cat Burton##http//.com/catburton?ffff
scala> val nameregex = """.*com/(.*)\?.*""".r
nameregex: scala.util.matching.Regex = .*com/(.*)\?.*
scala> ss match{ case nameregex(bc) => print(bc)}
catburton
正则语法请参考这个:
Scala - Regular Expressions