2016-11-11 1 views
0

여러 구분 기호로 한 줄씩 파일을 읽으려고합니다. 나는 분리를 위해 정규 표현식을 사용하고 있지만 분리 문자로 공간 ("")을 고려하지 않습니다. 파일에는;, # 및 스페이스가 구분 기호로 사용됩니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? , R1, R2, R3Java에서 여러 구분 기호로 파일 줄 읽기

public static void initialize() throws IOException { 
    PC = 4000; 
    BufferedReader fileReader = new BufferedReader(new FileReader("test/ascii.txt")); 
    String str; 
    while((str = fileReader.readLine()) != null){ 
     Instruction instruction = new Instruction(); 
     String[] parts = str.split("[ ,:;#]"); 
     instruction.instrAddr = String.valueOf(PC++); 
     System.out.println(instruction.instrAddr); 
     instruction.opcode = parts[0]; 
     System.out.println(instruction.opcode); 
     instruction.dest = parts[1]; 
     System.out.println(instruction.dest); 
     instruction.source_1 = parts[2]; 
     System.out.println(instruction.source_1); 
     instruction.source_2 = parts[3]; 
     System.out.println(instruction.source_2);  
    } 
    fileReader.close();} 

출력 인쇄 4000 (PC 값)를 추가하고, R1 ","R2 및 추가 -이 같은 파일 선 보인다. 공간을 피하는 방법? 정규식 str.split ("[, :; #]")에 문제가 있습니까? ?

+2

실제 ""문자 대신에 '\ s'을 (를) 사용해보십시오. – pathfinderelite

+0

문자 세트 대괄호 다음에'str.split ("[, :; #] +");를 시도해보십시오. – mgaert

+0

http://stackoverflow.com/questions/5200756/which-is-the-best-way-to-parse-a-file-containing-assembly-language-using-java – IceGlow

답변

0

실제로 공간이 맞습니까?

이 어떤 공백을 위해 일해야합니다

@Test 
public void test() { 
    String s = "1 2,3:4;5#6\t7"; 
    Assert.assertEquals(7, s.split("[\\s,:;#]").length); 
} 
관련 문제