how to read a structured file with java

1

I am trying to read a file with the following structure:

a{a,a,a,a,a,a,a,a,a,a,a,a},b{a,a,a,,a,a,a,a,a,a,a},c[c,c,c,c,c,,,,c,c,c,c]

I tried to do it with regular expressions and with split but nothing I could not achieve. Can someone help me with this?

    
asked by Jhonny Luis 17.12.2017 в 19:07
source

1 answer

1

With the help of regular expressions you can capture data group. A group is defined with the parentheses "()" and the matcher will capture what is inside. If we have parentheses nested as "(())" the macher will capture them in the hierarchical order, at some point I did something similar to what you need. I needed to read sets and their elements.

        String regex = "([a-z]+)\{([a-z,]+)\}";
        String line = "peliculas{flechaverde,batman}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(line);
        if(matcher.matches()){
            System.out.println(matcher.groupCount());
            for(int i = 0; i <= matcher.groupCount(); i++){
                System.out.println(matcher.group(i));
            }
        }

The output of these code lines:
2
movies {flechaverde, batman}
movies
flechaverde, batman

The last group could already apply a split (","), I think that group capture can be a good strategy to follow.

    
answered by 18.12.2017 в 02:48