2012-10-31 2 views
0

자바 IRC 라이브러리를 만들고 있는데 특정 사용자가 와일드 카드 * 문자를 포함 할 수있는 호스트 마스크와 일치하는지 확인하는 방법이 필요합니다. 정규식을 사용하지 않고 이것을 수행하는 가장 간단한 방법은 무엇입니까?자바 단순 와일드 카드 일치

몇 가지 예 :

// Anything works 
    * 
      server.freenode.net ✔ 

    // Any nickname/user/host works 
    *!*@*: 
      [email protected] ✔ 

    // Any nickname works (may have multiple nicknames on the same user) 
    *[email protected]/nebkat: 
      [email protected]/nebkat ✔ 
      [email protected]/nebkat ✔ 
      [email protected]/hacker ✘ 

    // Anything from an ip 
    *!*@123.4.567.89: 
      [email protected][email protected][email protected] ✘ 

    // Anything where the username ends with nebkat 
    *!*[email protected]* 
      [email protected]/nebkat ✔ 
      [email protected]/nebkat ✔ 
      [email protected]/nebkat ✘ 
+3

정규식을 사용하지 않는 이유는 무엇입니까? 이것은 정규 표현식과 완벽하게 일치하는 것처럼 보입니다. –

+0

@TomJohnson 특수 문자가 간섭 할 수 있다고 생각했기 때문에 처리해야합니다. 유일한 정규 표현식이 없으면 더 단순 할 것이라고 생각한 단 하나의 와일드 카드 문자 이후로? – nebkat

+0

당신은 항상 특수 문자를 이스케이프 할 수 있습니다 ... – jlordo

답변

2

이 함께 종료 :

public static boolean match(String host, String mask) { 
    String[] sections = mask.split("\\*"); 
    String text = host; 
    for (String section : sections) { 
     int index = text.indexOf(section); 
     if (index == -1) { 
      return false; 
     } 
     text = text.substring(index + section.length()); 
    } 
    return true; 
} 
+0

'host'가'abcdef'이고'mask'가'abc'인데도 그 함수는 여전히 "true"입니다. – Yonatan