0

I wrote a parser that reads a file line by line and parses it with a regex statement. (Regex below)

case "countries":
            pattern = "\\\"(.+?)\\\"(\\s+)?(\\((.+?)\\))?(\\s+)?(\\{(.+?)\\(\\#(.+?)\\)\\})?(\\s+)?(.+)";
            substitution = "$1, $4, $7, $8, $10";
            break;

This outputs a list with all the groups I want and each group separated by a comma. (through the result.split(",");) Now lets say I don't want to use a comma but instead an | or an *. Changing the comma to any other string doesn't seem to change anything. What am I missing?

try (CSVWriter csvWriter = new CSVWriter(new FileWriter(myLocalPath + "CSV/" + choice.toLowerCase() + ".csv")))
{
    Pattern r = Pattern.compile(pattern);

    while (br.readLine() != null)
    {
        String nextLine = br.readLine();

        Matcher matcher = r.matcher(nextLine);

        String result = matcher.replaceAll(substitution);

        String[] line = result.split("lorem");

        csvWriter.writeNext(line, false);
    }
}catch(Exception e){
    System.out.println(e);
    System.out.println("Parsing done!");
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Marco Geertsma
  • 803
  • 2
  • 9
  • 28

1 Answers1

0

seems what you're missing is Pattern.quote, if argument must be read literally, indeed split argument is a regex.

String[] line = result.split(Pattern.quote("..."));
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • Cool, i didnt know about the Pattern.quote part. Really neat to know however i dont think this is usefull for my application since my regex statement already performed as it should. Im already getting the results i want and in the groups i want. I just want to change the comma that gets added between each seperator to any other character. Still gonna look more at Pattern.quote though! – Marco Geertsma Jan 19 '18 at 13:40
  • about the choice of separator, in a csv it is generally `,` or `;` however if this caracter can appear in a field it must be escaped, commonly within enclosing quotes, it may be changed in CSVWriter settings – Nahuel Fouilleul Jan 19 '18 at 13:44