本人学习scala语言遇到的一些问题以及scala的一些巧妙用法
这篇文章主要是讲的字符串的操作
先看代码
val srcIp = """src=(\S+)""".r.findFirstMatchIn(message).flatMap { matcher => if(matcher.groupCount > 0) Some(matcher.group(1)) else None }.getOrElse("")
就上面的代码讲解下:
1."""src=(\S+)"""
其中双引号代表的用法:
多行字符串用三个双引号来表示分隔符,格式为:”“” … “”“;
2."""src=(\S+)""".r
r 的用法,将.r前的字符串作为正则表达式,返回类型为Regex.
官方API:scala.collection.immutable.StringLike
3..findFirstMatchIn(message)
findFirstMatchIn 方法:Regex.findFirstMatchIn (str)以Regex为正则表达式在字符串str中去匹配,返回 Option[Regex.Match]
官方API:scala.util.matching.Regex