开发者

Java Linked regex

I have to parse the following sample output. The requirements are there should be no text after Fabric management FPC state: i.e. it should be empty \s. The next part is a bit tricky and I am stuck there. So each FPC has one or more PFE and each FPE has one or more SIB. There are four possible states for each SIB. They are Plane Enabled, Link Error, Desination Error and Plane Disabled. I am supposed to parse this using regex and keep track of the state for each FPC, PFE and SIB. I am not sure how to have a 'linked' groups in regex.

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

So far what I have is

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");
                S开发者_JAVA技巧ystem.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.


I think instead of having one huge complex regex, I will prefer to do it in conditional loops with multiple regexes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜