方法一、通过转义符\转义
val str="{\“id”:\“12”,\“friends\”:{\“name\”:\“zs\”,\“age\”:\“40\”}}"
println(str)//{“id”:“12”,“friends”:{“name”:“zs”,“age”:“40”}}
方法二、""" … “”“来转义字符串
val str2=”""{“id”:“12”,“friends”:{“name”:“zs”,“age”:“40”}}"""
println(str2)//{“id”:“12”,“friends”:{“name”:“zs”,“age”:“40”}}
val reg = "[0-9]".r
//只找第一个匹配到的值
reg.findFirstMatchIn("asd2c3") match {
case Some(x) => println(x) //2
case None => println("no")
}
//分组用
println(reg.findAllMatchIn("asd2c3").toList)//List(2, 3)
//只拿字符串用
println(reg.findAllIn("ad2asd5a1sd2").toList)//List(2, 5, 1, 2)
练习1
我想取出属性的具体值 12、zs、40 ,下面是具体代码
val str="""{"id":"12","friends":{"name":"zs","age":"40"}}"""
val test="\\{\"id\":\"([0-9]+)\",\"friends\":\\{\"name\":\"([a-z]+)\",\"age\":\"([0-9]+)\"}}".r
println(test.findAllMatchIn(str).toList)//List({"id":"12","friends":{"name":"zs","age":"40"}})
test.findAllMatchIn(str).foreach(x=> println(x.group(1),x.group(2),x.group(3))) // (12,zs,40)
练习2
val r:Regex="([0-9a-zA-Z-#() ]+):([0-9a-zA-Z-#() ]+)".r
val input="name:Jason,age:19,weight:100"
for(x<-r.findAllMatchIn(input)){
//s"..${..}"固定写法
println(s"key: ${x.group(1)} value: ${x.group(2)}")
}
练习3
val d="""([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})""".r
"2014-05-06" match {
//跳过第一个,找第二个,放弃后面所有的
case d(_,month,_*) => println(month)//05
}
test文件下的内容
INFO 2000-01-07 requestURI:/c?app=0&p=1
INFO 2001-11-11 requestURI:/c?app=2&p=3
INFO 2011-12-02 requestURI:/c?app=0&p=3
ERROR 2002-11-17 requestURI:/c?app=1&p=3
package com.njbdqn.myscalafrst.regex
import scala.io.{BufferedSource, Source}
object IO {
def main(args: Array[String]): Unit = {
//读取文件test内容
var s: BufferedSource = Source.fromFile("C:\\Users\\wuyanxiang\\study\\hadoop\\myscalafirst\\src\\main\\scala\\com\\njbdqn\\myscalafrst\\regex\\test", "UTF-8")
//把读取的内容存入数组
val list = s.getLines().toListList(INFO 2000-01-07 requestURI:/c?app=0&p=1, INFO 2001-11-11 requestURI:/c?app=2&p=3, INFO 2011-12-02 requestURI:/c?app=0&p=3, ERROR 2002-11-17 requestURI:/c?app=1&p=3)
//解析类似字符串”INFO 2000-01-07 requestURI:/c?app=0&p=1“的正则表示式
val regex1 ="""([A-Z]+) ([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}) requestURI:(.*)""".r
list.map(line => line match {
case regex1(le, ld, ad) => println(le, ld, ad)
}
//(INFO,2000-01-07,/c?app=0&p=1)
//(INFO,2001-11-11,/c?app=2&p=3)
//(INFO,2011-12-02,/c?app=0&p=3)
//(ERROR,2002-11-17,/c?app=1&p=3)
)
}
}