Question:
What does the regular expression look like for this? I know about p{Punct}, but I don't know how to add other characters to it.
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader bfr = new BufferedReader(new FileReader(reader.readLine()));
BufferedWriter bfw = new BufferedWriter(new FileWriter(reader.readLine()));
String fileContent = "";
while (bfr.ready()) {
fileContent = fileContent + ((char) bfr.read());
}
String fileContentChanged = fileContent.replaceAll("\\p{Punct}", ""); /* в регулярке не хватает символов новой строки */
bfw.write(fileContentChanged);
reader.close();
bfr.close();
bfw.close();
}
}
PS I understand that you can just create a new line and change it with the next regular expression, but it looks like a crutch.
Answer:
you can use
"[\\p{Punct}\r\n]"
or
"[\\p{Punct}\\s&&[^\\h]]"
"[\\p{Punct}\r\n]"
matches all punctuation marks, as well as CR and LF characters.
"[\\p{Punct}\\s&&[^\\h]]"
matches all punctuation marks, as well as any "vertical" whitespace characters (i.e. all whitespace characters that the pattern \s
, except those which \h
finds, i.e. "horizontal" whitespace characters).