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.
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:
\\"\\\\".If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With