2013-10-15 2 views
0

저는 java와 regex의 초보자이며 일치하는 정규 표현식이 "A"또는 "OO"또는 "NC"인지 알고 싶습니다. 나는 그것을 텍스트 파일과 비교하고있다. 다른 스레드를 읽고 시도했지만 올바르게 할 수는 없습니다. 보고하지 않은 예외를 말하는 제 주요 방법에 문제가 있습니다.자바 정규식에 대한 일치 얻기

UserLogin.java

public static String userLoginVerify(String actNum,String pass) throws FileNotFoundException 
{ 
    Scanner scan = null; 
    AtmMenu atm = new AtmMenu(); 
    try { 
      scan = new Scanner(new BufferedReader(new FileReader("atm\\atmstatus.flat"))); 
      while(scan.hasNext()) 
      { 
       String lines; 
       lines = scan.nextLine(); 
       Pattern pattern = Pattern.compile("[A]||[OO]||[NC]"); 

         Matcher matcher = pattern.matcher(lines); 

       if(matcher.find()) { 
        System.out.println(matcher.group(1));  
        } 
      } 

    }catch(FileNotFoundException filenotfound) 
     { 
      System.out.println("File \"atmstatus.flat\" can't be found!"); 
     }  
    finally 
    { 
     if(scan != null) 
     scan.close(); 
     return "me"; 
    } 
} 

여기 mainmethod

public static void main(String[] login) 
{ 
    passLoginInfo(login[0],login[1]); 

} 

public static void passLoginInfo(String accountNumber, String password) 
{ 
    UserLogin userLogin = new UserLogin(); 
    userLogin.userLoginVerify(accountNumber, password); 

} 

Here'd 나는이 통계에 편지를 확인하고

currentamount    | stat 
P500,000,000,000,000,000.00 | A 

을 확인하고 있습니다 파일

+0

그것은 당신이 정규식으로 수행 할 작업을 매우 불분명 -하지만 예외 문제의 측면에서 - 확인 된 예외를 얼마나 이해합니까? http://docs.oracle.com/javase/tutorial/essential/exceptions/ –

+0

문자열이 "A", "OO"또는 "NC"인지 확인하고 싶습니까? – Michael

+0

예 String과 일치하는지 확인하고 싶습니다. 나는 체크하고있는 파일을 게시 할 것이다 – Ley47

답변

0
ONL 인 경우

나는 내 문제를 해결하고 여기에 내가 무슨 짓을했는지

?.*\\s*A|OO|NC\\s*.*$ 
+0

나는 그것을 시도했지만 여전히 동일하다 – Ley47

0

: A, OO 및 NC에 대한 y를 누른 후, 당신은으로 정규식을 가질 수

scan = new Scanner(new BufferedReader(new FileReader("atm\\atmstatus.flat"))); 
      while(scan.hasNext()) 
      { 
       String lines; 
       lines = scan.nextLine(); 
       System.out.println(lines[2]); 
       Pattern pattern = Pattern.compile("[A]|[OO]|[NC]");    
         Matcher matcher = pattern.matcher(lines);            
       if(matcher.find()) { 
        System.out.println(matcher.group(0));  
        } 
      } 
+1

사실, 의도 한대로 작동하지 않을 것이다. ''A '', ''O '', ''C ''패턴은 문자열 "A", "O", "N" '또는''NC ''라고 표시합니다. 왜냐하면'[]'는 문자 클래스를 기술하기 때문에 "내 수업의 문자 중 하나"가 될 것입니다. 실제로 원하는 것은''(A) | (OO) | (NC)''입니다. – blalasaadri

+0

감사합니다. :) – Ley47

관련 문제