2

I'm trying to split lines in this file eng-pol.txt by new line symbol "\n" and it's simply not working... I've tried:

String[] words = strLine.split("\n");
System.out.println(Arrays.toString(words));
[abbot    abbot [ˈæbət] <N>\n opat]

String[] words = strLine.split("\\n");
System.out.println(Arrays.toString(words));
[abbot    abbot [ˈæbət] <N>\n opat]

String[] words = strLine.split("\\r|\r");
System.out.println(Arrays.toString(words));
[abbot    abbot [ˈæbət] <N>\n opat]

Even this:

String[] words = strLine.split(System.getProperty("line.separator"));
System.out.println(Arrays.toString(words));
[abbot    abbot [ˈæbət] <N>\n opat]

I'm reading the file using BufferedReader and readLine method.

bartektartanus
  • 12,415
  • 4
  • 70
  • 92

1 Answers1

9

I'm not sure if I understand you correctly but I see that your file contains \n literals (not actual line breaks). If you want to split on them then you need to write split as

split("\\\\n")

Regex which represents \ literal looks like \\ (it needs to be escaped, otherwise single \ will be treated for example as start of predefined character class like \d which represents digits).

But String which will represents such regex \\ need to be written as "\\\\" because in string \ is also special and it also needs to be escaped.

In short to match \ with regex you need to escape it in two levels:

  • in regex \\
  • in String "\\\\".
Pshemo
  • 113,402
  • 22
  • 170
  • 242