오늘 밤 저는 파일에서 단어를 구문 분석하려고 시도하고 있습니다. 공백뿐만 아니라 대문자와 소문자 단어를 보존하면서 모든 구두점을 제거하고 싶습니다.구두점을 제거하고 글자와 공백을 보존하십시오 - Java Regex
String alpha = word.replaceAll("[^a-zA-Z]", "");
공백을 포함하여 모든 것을 바꿉니다.
Testing, testing, 1, one, 2, two, 3, three.
포함 된 텍스트 파일을 조작, 출력은 내가 출력은 변경되지 않습니다
String alpha = word.replaceAll("[^a-zA-Z\\s]", "");
로 변경할 때, 그러나 TESTINGTESTINGONETWOTHREE
된다.
여기 전체에서이 코드입니다 : 읽기,
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 Scanner(new File(targetFile));
while(fileScan.hasNext()){
String word = fileScan.next();
//Replace non-alphabet characters with empty char.
String alpha = word.replaceAll("[^a-zA-Z\\s]", "");
System.out.print(alpha.toUpperCase());
}
fileScan.close();
}
}
file.txt를 가지고 한 줄 Testing, testing, 1, one, 2, two, 3, three.
내 목표 출력 Testing Testing One Two Three
난 그냥 정규 표현식에서 뭔가 잘못하고 있습니까 읽을 수있다, 아니면 제가해야 할 일이 있습니까? 관련성이있는 경우 32 비트 Eclipse 2.0.2.2에서 작업하고 있습니다.
감사합니다 모든 문장 부호를 제거
를 지원하는지 생각합니다. 이 코드는 대문자 단어 만 다루는 단어 색인 프로그램의 일부가 될 것이므로이 후에 어쨌든 문자열은 toUpperCase()를 통해 전달됩니다. 실제로 코드를 통해 출력을 재현 할 수는 없습니다. 전체 코드를 원래 질문으로 편집합니다. – alldavidsluck
내 코드를 질문에 편집했습니다. 직접 정의 된 문자열을 사용하여 조각을 가져올 수 있었지만 문자열이 파일에서 나온다면 나는 이상하게 느껴지지 않습니다. – alldavidsluck