2013-03-28 2 views
0

카드 리더기를 emplementing하고 있고 Android에서 정규 표현식을 사용해야합니다. 위키 피 디아 다음 TRACK (트랙)에 대한 정규식은 다음과 같습니다Android regex - credita card track

^%([A-Z])([0-9]{1,19})\^([^\^]{2,26})\^([0-9]{4}|\^)([0-9]{3}|\^)([^\?]+)\?$ 
여기 시도

: 다음 예와 http://www.regexplanet.com/advanced/java/index.html : %의 B6011898748579348^DOE/JOHN^37,829,821,000,123,456,789? 및 내 응용 프로그램에서 작동하지만 않습니다.

String re = "%([A-Z])([0-9]{1,19})\\^([^\\^]{2,26})\\^([0-9]{4}|\\^)([0-9]{3}|\\^)([^\\?]+)\\?"; 
Pattern p = Pattern.compile(re); 
String teste = "%B6011898748579348^DOE/ JOHN    ^3782982100?"; 
Matcher m = p.matcher(teste); 
    Log.d(TAG,"1111: "+m.groupCount()); 
int i=0; 
for(i=0;i<m.groupCount();i++){ 
    try{ 
     Log.d(TAG, "GROUP"+Integer.toString(i)+" - "+m.group(i)); 
    }catch (IllegalStateException e){ 
    Log.d(TAG, e.toString()); 
    } 
} 

Teste^및 $와 여러하지만 아무도이 일에 : 결과는 항상의 :

1111 : 6 java.lang.IllegalStateException : 없음 성공적으로 일치 지금까지 java.lang.IllegalStateException : 아니오 ... ...

답변

1

먼저 m.find()을 사용해야합니다. 또한 마지막 그룹을 포함하여 반복해야합니다. 이 방법을 시도하십시오

... 
if(m.find()){ 
    Log.d(TAG,"1111: " + m.groupCount()); 

    //change '<' into '<=' to include group 6 
    for(int i=0; i<=m.groupCount(); i++){ 
     try{ 
      Log.d(TAG, "GROUP"+Integer.toString(i)+" - "+m.group(i)); 
     }catch (IllegalStateException e){ 
      Log.d(TAG, e.toString()); 
     } 
    } 
} 
+0

고마워 :) :) –