scala IO 正则 日志解析

日志

INFO 2016-07-25 requestURI:/c?app=0&p=1&did=18005472&industry=469&adid=31
INFO 2016-07-25 requestURI:/c?app=0&p=2&did=18005472&industry=469&adid=31
INFO 2016-07-25 requestURI:/c?app=0&p=1&did=18005472&industry=469&adid=32

代码

object IODemo extends App {
  //输入输出流 IO
  //Java中有字符 字节输入输出
  val filePath:String="src/log.log"
//  private val reader: BufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"GBK"))
//  println(reader.readLine())
  val file: BufferedSource = Source.fromFile(filePath)
//  file.getLines().foreach(println)
//  "INFO 2016-07-25 requestURI:/c?app=0&p=1&did=18005472&industry=469&adid=31"
//  val regex: Regex = """([a-zA-Z]+) (\d\d\d\d-\d\d-\d\d) [^:]*:(.*)""".r()
//  val regex: Regex = """(\w+) (\d{4}-\d{2}-\d{2}) [^:]*:(.*)""".r()
  val regex: Regex = """(.*?) (.*?) .*?:(.*)""".r()

  //匹配获取相关内容,使用正则表达式,使用模式匹配
  for (s<- file.getLines()){
    s match {
      case regex(level,date,uri) =>println(s"日志级别:$level, 日期:$date, uri:$uri ")
      case _=>println("匹配不到")
    }
  }
}

你可能感兴趣的:(scala,正则表达式)