50,539
社区成员
发帖
与我相关
我的任务
分享
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import static java.nio.file.StandardOpenOption.*;
//JDK 1.7以上支持.
/*
---------- java ----------
ikt4mvbb-2
ikm5iwa6-2
Output completed (0 sec consumed) - Normal Termination
*/
public class Test{
public static void main(String[] args){
String content = loadLog("work.log");
String regex = "(?s)(?<year>\\d+)-(?<month>\\d+)-(?<date>\\d+) (?<hour>\\d+):(?<minute>\\d+):(?<second>\\d+).*?([_#])(?<value>[^_#]+)\\7sdrmgr[.]NodeMe";
Matcher matcher = Pattern.compile(regex).matcher(content);
while(matcher.find()){//这里采用find
System.out.println(matcher.group("value"));
}
}
private static String loadLog(String srcPath){
//从对应的文件路径中读取log信息.(可包含多条记录)
Path src = Paths.get(srcPath);
String content = null;
try(FileChannel channel = (FileChannel)(Files.newByteChannel(src,READ));){
int size = (int)channel.size();
ByteBuffer buf = ByteBuffer.allocate(size);
channel.read(buf);
buf.flip();
Charset charset = Charset.defaultCharset();
CharsetDecoder decoder = charset.newDecoder();
content = decoder.decode(buf).toString();
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
return content;
}
}
/* work.log 信息:
2016-02-19 14:44:09,668 INFO [PoProcessor_PROC_L_CELL_CDMA_HO37332] process 7: 37332, 201602190200+0800#20160219144409#ikt4mvbb-2#sdrmgr.NodeMe#37332#PROC_L_CELL_CDMA_HO#1.csv, Cell(LTE)_20160219021528967+080024B20160219.0200+0800-20160219.0215+0800_0_1_780-3143_180_0_ltefdd.zip, 31K. result: OK
2016-02-19 14:41:50,090 INFO [com.zte.ums.minos.slave.pm.emf.datacollect.fileparser.RptFileProcessor_CSV_ikm5iwa6-2_sdrmgr.NodeMe_sdrmgr.PlatIPPD] process 7: finish post-process file: IPPath(PLAT)_20160219144638719+080024A20160219.1430+0800-20160219.1445+0800_0_2_1000-3_0_0.zip, result: empty.
*/
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE regex [
<!ELEMENT regex (code)>
<!ELEMENT code (#PCDATA)>
]>
<regex>
<code>
<![CDATA[
(?s)(?<year>\\d+)-(?<month>\\d+)-(?<date>\\d+) (?<hour>\\d+):(?<minute>\\d+):(?<second>\\d+).*?(?<value>\\S+)_\\1\\2\\3
]]>
</code>
</regex>
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//JDK 1.7以上支持.
public class Test{
public static void main(String[] args){
//String content = "2016-02-19 14:41:46,649 INFO process 7: 37002, #1.csv, EthPort(PLAT)_20160219144638716";
String content = "2016-02-19 14:41:50,090 INFO process 7: finish post-process file: CELL(PLAT)_20160219144638719+0800";
String regex = "^(?<year>\\d+)-(?<month>\\d+)-(?<date>\\d+) (?<hour>\\d+):(?<minute>\\d+):(?<second>\\d+).*?(?<value>\\S+)_\\1\\2\\3.*$";
Matcher matcher = Pattern.compile(regex).matcher(content);
if(matcher.matches()){
content = matcher.group("value");
}
System.out.println(content);
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import static java.nio.file.StandardOpenOption.*;
//JDK 1.7以上支持.
public class Test{
public static void main(String[] args){
String content = loadLog("work.log");
String regex = "(?s)(?<year>\\d+)-(?<month>\\d+)-(?<date>\\d+) (?<hour>\\d+):(?<minute>\\d+):(?<second>\\d+).*?(?<value>\\S+)_\\1\\2\\3";
Matcher matcher = Pattern.compile(regex).matcher(content);
while(matcher.find()){//这里采用find
System.out.println(matcher.group("value"));
}
}
private static String loadLog(String srcPath){
//从对应的文件路径中读取log信息.(可包含多条记录)
Path src = Paths.get(srcPath);
String content = null;
try(FileChannel channel = (FileChannel)(Files.newByteChannel(src,READ));){
int size = (int)channel.size();
ByteBuffer buf = ByteBuffer.allocate(size);
channel.read(buf);
buf.flip();
Charset charset = Charset.defaultCharset();
CharsetDecoder decoder = charset.newDecoder();
content = decoder.decode(buf).toString();
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
return content;
}
}
/* work.log 信息:
2016-02-19 14:44:09,668 INFO [PoProcessor_PROC_L_CELL_CDMA_HO37332] process 7: 37332, 201602190200+0800#20160219144409#ikt4mvbb-2#sdrmgr.NodeMe#37332#PROC_L_CELL_CDMA_HO#1.csv, Cell(LTE)_20160219021528967+080024B20160219.0200+0800-20160219.0215+0800_0_1_780-3143_180_0_ltefdd.zip, 31K. result: OK
2016-02-19 14:41:50,090 INFO [com.zte.ums.minos.slave.pm.emf.datacollect.fileparser.RptFileProcessor_CSV_ikm5iwa6-2_sdrmgr.NodeMe_sdrmgr.PlatIPPD] process 7: finish post-process file: IPPath(PLAT)_20160219144638719+080024A20160219.1430+0800-20160219.1445+0800_0_2_1000-3_0_0.zip, result: empty.
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//JDK 1.7以上支持.
public class Test{
public static void main(String[] args){
//以下是模拟XML中获取标签值中指定内容.(注:标签中未实现可含参数,如果需要包含参数需要重新修改)
String content = "<src>2016-02-19 14:41:46,649 INFO process 7: 37002, #1.csv, EthPort(PLAT)_20160219144638716</src>";
//String content = "<content >2016-02-19 14:41:50,090 INFO process 7: finish post-process file: CELL(PLAT)_20160219144638719+0800</content>";
String regex = "^\\s*<(?<tagName>[^>]+)\\s*>(?<year>\\d+)-(?<month>\\d+)-(?<date>\\d+) (?<hour>\\d+):(?<minute>\\d+):(?<second>\\d+).*?(?<value>\\S+)_\\2\\3\\4.*</\\1\\s*>\\s*$";
Matcher matcher = Pattern.compile(regex).matcher(content);
if(matcher.matches()){
content = matcher.group("value");
}
System.out.println(content);
}
}