如何从文本中删除包含破折号的单词?
How do I remove a word that contains a dash from a text?
所以我想删除一个在它们之间包含破折号的单词,例如 off-campus 以及我想删除的其他内容。到目前为止,这是我的代码。
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);
到目前为止它打印
CS is a wonderful class So WONDERFUL amazing class cleaver class must-remove
您可以使用正则表达式 \w+-\w+
,这意味着 2 个单词 \w+
,由破折号分隔,然后您使用
"CS 204 is a wonderful class. So WONDERFUL ! must-remove".replaceAll("\w+-\w+", "")
// CS 204 is a wonderful class. So WONDERFUL !
所以我想删除一个在它们之间包含破折号的单词,例如 off-campus 以及我想删除的其他内容。到目前为止,这是我的代码。
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);
到目前为止它打印
CS is a wonderful class So WONDERFUL amazing class cleaver class must-remove
您可以使用正则表达式 \w+-\w+
,这意味着 2 个单词 \w+
,由破折号分隔,然后您使用
"CS 204 is a wonderful class. So WONDERFUL ! must-remove".replaceAll("\w+-\w+", "")
// CS 204 is a wonderful class. So WONDERFUL !