最近项目中用到很多正则,所以想把正则这块给好好整理下,首先想写最近项目常用到的,后期会从基础语法开始写起。废话不多说干货。。。
//1、对 /(斜杠) 进行截取
String fileName = "data/2012/09/18/20120918.txt";
String[] ary = fileName.split("/");
System.out.println(ary[ary.length-1]);
//2、对 .(点) 进行截取
String name = "20120918.txt";
String [] strArr = name.split("\\.");
System.out.println(strArr[0]);
//3、对 \(反斜杠) 进行截取
// String fileNameBack = "data\2012\09\18\20120918.txt";
String fileNameBack = "data\\2012\\09\\18\\20120918.txt";
// String fnb = fileNameBack.replace("\\\\","\\\\");
// System.out.println(fnb);
String[] temps = fileNameBack.split("\\\\");
System.out.println(temps[temps.length-1]);
//4、对,(逗号) 、空格 进行截取
String thingName = "2915211100,2915211900,2915219010,2915219020,海洛因";
String[] thingNameArr = thingName.split(",");
System.out.println(thingNameArr[thingNameArr.length-1]);
String spaceName = "2915211100 乙醇";
String[] spaceNameArr = spaceName.split(" ");
System.out.println(spaceNameArr[spaceNameArr.length-1]);
原文链接:/regex/357878.html