so I want to remove a word that contains a dash between them like off-campus alongside other things that I want to remove. here is my code so far.
Scanner read = new Scanner(System.in); String text; //text: CS 204 is a wonderful class. So WONDERFUL ! amazing class. cleaver class. must-remove System.out.print("Please Enter Text: "); text = read.nextLine(); System.out.println(text); String n = text.replaceAll("[\.\!\d]", ""); System.out.println(n);
so far it prints
CS is a wonderful class So WONDERFUL amazing class cleaver class must-remove
Advertisement
Answer
You could use the regex w+-w+
, which means 2 words w+
, separated by a dash, and you use
"CS 204 is a wonderful class. So WONDERFUL ! must-remove".replaceAll("\w+-\w+", "") // CS 204 is a wonderful class. So WONDERFUL !