package
com.jn.scala.basics
/**
* 正则表达式
* Created by admin on 2015/11/22.
*/
object RegExpressOps {
def main(args: Array[ String ]) {
val regex = """([0-9]+) ([a-z]+)""" .r //这样就变成了一个正则表达式
val numPattern = "[0-9]+" .r
val numberPattern = """\s+[0-9]+\s+""" .r
//findAllIn方法进行遍历匹配,注意这里在写的时候555和666前面空格,如果没有那么是没有值的
for (matchString <- numberPattern.findAllIn( " 555 Scala, 666 Spark" ))
println(matchString)
// 555
// 666
//找到首个匹配
println(numberPattern.findFirstIn( "88ss,java, 222 hadoop" ))
//Some( 222 )
val numitemPatern = """([0-9]+) ([a-z]+)""" .r
val numitemPatern(num, item) = "88 hadoop"
val line = "666 spark" //这里用到了模式匹配
line match {
case numitemPatern(num, blog) => println (num + " \t " + blog)
case _ => println( "oop..." )
}
// 666 spark
}
/**
* 正则表达式
* Created by admin on 2015/11/22.
*/
object RegExpressOps {
def main(args: Array[ String ]) {
val regex = """([0-9]+) ([a-z]+)""" .r //这样就变成了一个正则表达式
val numPattern = "[0-9]+" .r
val numberPattern = """\s+[0-9]+\s+""" .r
//findAllIn方法进行遍历匹配,注意这里在写的时候555和666前面空格,如果没有那么是没有值的
for (matchString <- numberPattern.findAllIn( " 555 Scala, 666 Spark" ))
println(matchString)
// 555
// 666
//找到首个匹配
println(numberPattern.findFirstIn( "88ss,java, 222 hadoop" ))
//Some( 222 )
val numitemPatern = """([0-9]+) ([a-z]+)""" .r
val numitemPatern(num, item) = "88 hadoop"
val line = "666 spark" //这里用到了模式匹配
line match {
case numitemPatern(num, blog) => println (num + " \t " + blog)
case _ => println( "oop..." )
}
// 666 spark
}
}