最全的常用正则表达式大全——包括校验数字、字符、一些特殊的需求等等
java正则表达式和linux表达式是有一些不一样的
def test11(): Unit ={ val date = "2017-08-09" val warn = "WARN" val strs = Array("2017-08-09 10:24:11,040 RN org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode: Checkpoint done. New Image Size: 117430", "2017-08-09 11:24:11,570 WARN org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode: Checkpoint done. New Image Size: 117230", "2017-08-09 11:24:11,570 WA org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode: Checkpoint done. New Image Size: 117230", "2017-08-09 11:24:11,570 WARN org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode: Checkpoint done. New Image Size: 117230") strs.filter(_.matches(".*2017-08-09.*WARN.*")).foreach(println(_)) }
比如,匹配一个日期和单词, scala需要这样写
_.matches(".*2017-08-09.*WARN.*")
shell中需要这样写
cat /home/hadoop/apps/hadoop-2.6.5/logs/hadoop-hadoop-secondarynamenode-mini1.log | grep '2017-08-09.*WARN'
在Java中默认是以要匹配的单词开头的,而linux 不是所以java中需要加上.*