今晚我试图从文件中解析单词,我想删除所有标点符号,同时保留大小写单词以及空格。
String alpha = word.replaceAll("[^a-zA-Z]", "");
Run Code Online (Sandbox Code Playgroud)
这将替换所有内容,包括空格。
对包含 的文本文件进行操作Testing, testing, 1, one, 2, two, 3, three.,输出变为TESTINGTESTINGONETWOTHREE
但是,当我将其更改为
String alpha = word.replaceAll("[^a-zA-Z\\s]", "");
Run Code Online (Sandbox Code Playgroud)
输出不会改变。
这是完整的代码片段:
public class UpperCaseScanner {
public static void main(String[] args) throws FileNotFoundException {
//First, define the filepath the program will look for.
String filename = "file.txt"; //Filename
String targetFile = "";
String workingDir = System.getProperty("user.dir");
targetFile = workingDir + File.separator + filename; //Full filepath.
//System.out.println(targetFile); //Debug code, prints the filepath.
Scanner fileScan = new …Run Code Online (Sandbox Code Playgroud)