Nabarko Roy
Nabarko Roy

Reputation: 19

Parsing an output by Java

How can i parse an output like:(given below) String to be parsed

I need the "POLICY ENM_USERDEFINED_wtc2e3lms-bu" and the value of the "EXIT STATUS" only. There are similar outputs also present but at different positions. So cannot use substring or split. Parsing has to be done by Java.

This is an output of a command and hence need the similar "POLICY" name and value of "EXIT STATUS Output Command

Upvotes: 0

Views: 416

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

You could use a couple of regular expressions to find and return the text your after.

For example, using...

public String find(String pattern, String in) {
    Pattern p = Pattern.compile(pattern);
    Matcher matcher = p.matcher(in);
    if (matcher.find()) {
        return matcher.group();
    }
    return null;
}

And...

String value = "some preable POLICY ENM_USERDEFINED_wtc2e3lms-bu some postable EXIT STATUS 100 some other stuff thrown in the mix";
System.out.println(find("POLICY ENM[a-zA-Z_0-9-]+", value));
System.out.println(find("EXIT STATUS [0-9]+", value));

Outputs...

POLICY ENM_USERDEFINED_wtc2e3lms-bu
EXIT STATUS 100

From there, I'd use another regular expression to extract the actual status value ... but I'm sure you can figure that part out ;)

Upvotes: 2

Related Questions