java – How to remove all punctuation marks and newlines from a string?


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(;
            BufferedReader bfr = new BufferedReader(new FileReader(reader.readLine()));
            BufferedWriter bfw = new BufferedWriter(new FileWriter(reader.readLine()));
            String fileContent = "";
            while (bfr.ready()) {
                fileContent = fileContent + ((char);
            String fileContentChanged = fileContent.replaceAll("\\p{Punct}", ""); /* в регулярке не хватает символов новой строки */

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.


you can use




"[\\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).

Scroll to Top