1
我想掃描一個文本文件,只打印出包含字母的字符串,使用正則表達式。無法使用Java和正則表達式打印文件
當我使用正則表達式時,我當前的代碼一直返回false。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DocumentReader {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File("document.txt"); // document
Scanner in = new Scanner(inputFile);
while (in.hasNext()){
String input = in.next();
input.matches("([^a-zA-Z])");
System.out.println(input.matches("[^a-zA-Z]"));
System.out.println(input);
//if text matches regex, print text
if (input.matches("[^a-zA-Z]")){
System.out.println(input);
}
}
}
}
你是指'[a-zA-Z] +' –