[英]java parser file text
i have this text file and i wanna parser and put it into mysql database: 我有这个文本文件,我想解析器并将其放入mysql数据库:
A:PE9301_P# show port 1/1/1
===============================================================================
Ethernet Interface
===============================================================================
Description : PE9301_P:PE9148_P:01:10G
Interface : 1/1/1 Oper Speed : 10 Gbps
Link-level : Ethernet Config Speed : N/A
Admin State : up Oper Duplex : full
Oper State : up - Active in LAG 1 Config Duplex : N/A
Physical Link : Yes MTU : 9212
Single Fiber Mode : No Min Frame Length : 64 Bytes
and i want just those info: PE9301,up - Active in LAG 1 i can't find a solution help mz plz 我只需要这些信息:PE9301,向上-处于滞后1状态,我找不到解决方案帮助mz plz
Use the following regexp: 使用以下正则表达式:
/Description\s+:\s([\w\s]+)/g
and 和
/Oper State\s+:\s(.{26,26})/g
If you want to parse the whole structure, use character length rather than regular expressions to split the structure into key-value array, and compare keys to constants ("Description", "Oper State", etc...) 如果要解析整个结构,请使用字符长度而不是正则表达式将结构拆分为键值数组,然后将键与常量(“描述”,“操作状态”等)进行比较。
A working exemple as a Java test unit : 作为Java测试单元的工作示例:
private static final String NIFSOURCE = "A:PE9301_P# show port 1/1/1\r\n"+
"===============================================================================\r\n"+
"Ethernet Interface\r\n"+
"===============================================================================\r\n"+
"Description : PE9301_P:PE9148_P:01:10G\r\n"+
"Interface : 1/1/1 Oper Speed : 10 Gbps\r\n"+
"Link-level : Ethernet Config Speed : N/A\r\n"+
"Admin State : up Oper Duplex : full\r\n"+
"Oper State : up - Active in LAG 1 Config Duplex : N/A\r\n"+
"Physical Link : Yes MTU : 9212\r\n"+
"Single Fiber Mode : No Min Frame Length : 64 Bytes";
/**
* Ceci est un test que l'ont peut remplacer par une méthode Main ou une function
* - Uncle Bob Martin
*/
@Test
public void NIFParseTest() {
String NIFdescription = null, NIFstate = null;
Pattern motifRE = Pattern.compile("^Description\\s+:\\s(\\w+)", Pattern.MULTILINE);
Pattern stateRE = Pattern.compile("^Oper State\\s+:\\s(.{26,26})", Pattern.MULTILINE | Pattern.UNIX_LINES);
Matcher foundMatch = motifRE.matcher(NIFSOURCE);
if (foundMatch.find()) {
NIFdescription = foundMatch.group(1); //0 est la chaine complète, les motifs entre parenthèse sont disponibles à l'index 1
}
foundMatch = stateRE.matcher(NIFSOURCE);
if (foundMatch.find()) {
NIFstate = foundMatch.group(1);
}
assertNotNull(NIFdescription);
assertNotNull(NIFstate);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.