我必须解析以下示例输出。要求是Fabric management FPC state:
后不应有任何文本,即\s
为空。下一部分有点棘手,我被困在那里。因此,每个FPC
具有一个或多个PFE
,每个FPE
具有一个或多个SIB
。每个SIB
都有四种可能的状态。他们是Plane Enabled, Link Error, Desination Error and Plane Disabled
。我应该使用正则表达式来解析它,并跟踪每个FPC, PFE and SIB
的状态。我不确定如何在正则表达式中包含'linked'
组。
Fabric management FPC state:
FPC #0
PFE #0
SIB #0
Plane enabled
SIB #1
Link Error
PFE #1
SIB #0
Destination Error
SIB #1
Plane Disabled
SIB #2
Plane enabled
FPC #1
PFE #1
SIB #0
Plane enabled
到目前为止,我所拥有的是
public void parseFPCS(String commandOutput) {
regex = "FPC state:(\\s*)(FPC\\s*#?\\d+)\\s*(PFE\\s*#\\d+)\\s*(SIB\\s*#\\d+)\\s*(\\w*\\s*\\w*)";
pattern = Pattern.compile(regex, patternFlag);
matcher = pattern.matcher(commandOutput);
while(matcher.find()) {
String empty = matcher.group(1);
Boolean isEmpty = empty.trim().isEmpty();
if(isEmpty) {
System.out.println("Link Empty");
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(5));
//Right now I am just printing it out to see the outcome.
}
}
The current outcome is
Link Empty
FPC #0
PFE #0
SIB #0
Plane enabled //This is expected.
最佳答案
我认为与其拥有一个庞大的复杂正则表达式,不如在具有多个正则表达式的条件循环中执行此操作。
关于java - Java链接正则表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7688867/