Taktech
Taktech

Reputation: 485

parsing complex text-line and save each entry to a variable - java

I have auto-generated lines like the following:

  40   1655.28   -------   212.61     2.01   ( 40.31%) (  9)STRE C=C (aminoA - part A) -> 1

is their any rational and "easy" way to split the previous line other that line.split("\\s+");? and get something like the following:

a0=40
a1=1655.28

...

ai=40.31
aii=9
aiii=STRE C=C
aiv=aminoA - part A

The problem with line.split("\\s+"); is that I have round bracket without spaces between two entries; e.g.:( 9)STRE C=C or (aminoA - part A), which gives results like: ax1=(,ax2=9)STRE and ay1=(aminoA. This of course could be caught, but I need in this case to check each entry by multiple regex in - for me - complex-nested if-conditions.

Upvotes: 0

Views: 106

Answers (1)

TedTrippin
TedTrippin

Reputation: 3662

So, without fixed columns then I would use regex. I would also use a class to hold your columns for ease of lookup.

class Result {

    private static Pattern resultRegex = Pattern.compile(
        "(\\d+)" // 40
        + "\\s+"
        + "(\\d+\\.\\d+)" // 1655.28
        + "\\s+\\-+\\s+"
        + "(\\d+\\.\\d+)" // 212.61
        + "\\s+"
        + "(\\d+\\.\\d+)" // 2.01
        + "\\s+\\(\\s*" // (
        + "(\\d+\\.\\d+)" // 40.31
        + "%\\)\\s+\\(\\s*" // ) (
        + "(\\d)" // 9
        + "\\)" // )
        + "(.*)" // STRE C=C
        + "\\(" // (
        + "(.*)" // aminoA - part A
        + "\\)"); // )

    private final int col0;
    private final double col1;
    private final double col2;
    private final double col3;
    private final double col4;
    private final int col5;
    private final String col6;
    private final String col7;

    Result(String data) {

        Matcher matcher = resultRegex.matcher(data);
        matcher.find();
        col0 = Integer.parseInt(matcher.group(1));
        col1 = Double.parseDouble(matcher.group(2));
        col2 = Double.parseDouble(matcher.group(3));
        col3 = Double.parseDouble(matcher.group(4));
        col4 = Double.parseDouble(matcher.group(5));
        col5 = Integer.parseInt(matcher.group(6));
        col6 = matcher.group(7);
        col7 = matcher.group(8);
    }

    @Override
    public String toString() {
        return new StringBuilder()
            .append("col0=").append(col0)
            .append(",col1=").append(col1)
            .append(",col2=").append(col2)
            .append(",col3=").append(col3)
            .append(",col4=").append(col4)
            .append(",col5=").append(col5)
            .append(",col6=").append(col6)
            .append(",col7=").append(col7).toString();
    }
}

Then you can read each line from your file/stream/whatever and do this

String data = "40   1655.28   -------   212.61     2.01   ( 40.31%) (  9)STRE C=C (aminoA - part A) -> 1";
Result result = new Result(data);
System.out.println(result);

Obviously, I haven't added any validation but hopefully that's a good enough example to get started.

Upvotes: 1

Related Questions