2013-07-23 7 views
3

나는 MessageFormat을 이렇게 가지고있다;문자열이 Java의 특정 MessageFormat과 일치하는지 확인하십시오.

final MessageFormat messageFormat = new MessageFormat("This is token one {0}, and token two {1}"); 

나는 일부 문자열이 있는지 궁금합니다. 위의 문자열은 MessageFormat의를 사용하여 만든 경우

String shouldMatch = "This is token one bla, and token two bla"; 
String wontMatch = "This wont match the above MessageFormat"; 

어떻게 확인합니까? 나는. 그것들은 messageFormat과 일치합니까?

감사합니다.

+0

저는 이해가 안됩니다. 문자열이 메시지 형식으로 작성하는 것과 비슷합니까? 이것이 질문이라면, 문자열에 mesagge 형식을 구문 분석하고 같음을 수행하십시오. – Deckard27

답변

6

Regular ExpressionsPatternMatcher 클래스를 사용하면됩니다. 간단한 예 : 정규식의

Pattern pat = Pattern.compile("^This is token one \\w+, and token two \\w+$"); 
Matcher mat = pat.matcher(shouldMatch); 
if(mat.matches()) { 
    ... 
} 

설명 :

Pattern pat = Pattern.compile("^This is token one (\\w+), and token two (\\w+)$"); 

당신은 mat.group(1)를 사용하여 그룹을 검색 할 수 있습니다 : 당신이 토큰을 캡처하려면 다음과 같이 괄호를 사용하는 경우

^ = beginning of line 
\w = a word character. I put \\w because it is inside a Java String so \\ is actually a \ 
+ = the previous character can occur one ore more times, so at least one character should be there 
$ = end of line 

mat.group(2).

0

는 별도로 등록 전에서 그것은 내가 단지 개발이 코드로도 수행 할 수 있습니다 답변 :

public class Points { 
    private int start; 

    public int getStart() { 
     return start; 
    } 

    public void setStart(int start) { 
     this.start = start; 
    } 
    private int end; 

    public int getEnd() { 
     return end; 
    } 

    public void setEnd(int end) { 
     this.end = end; 
    } 
} 

public class Split { 
    public static void main(String[] args) { 
     final MessageFormat messageFormat = new MessageFormat("This is token one {0}, and token two {1}"); 

     ArrayList<String> list = new ArrayList<String>(); 
     list.add("This is token one bla, and token two bla"); 
     list.add("This wont match the above MessageFormat"); 
     list.add("This wont match the above MessageFormat"); 
     list.add("This wont match the above MessageFormat"); 
     list.add("This is token one bla, and token two bla"); 
     list.add("This wont match the above MessageFormat"); 

     Format[] format = messageFormat.getFormats(); 
     int del = format.length; 

     ArrayList<String> delimeters = new ArrayList<String>(); 
     for (int i = 0; i < del; i++) { 
      delimeters.add("{" + i + "}"); 
     } 
     //System.out.println(messageFormat.toPattern()); 
     ArrayList<Points> points = new ArrayList<Points>(); 
     int counter = 0; 
     for (String x : delimeters) { 
      Points tmp = new Points(); 
      tmp.setStart(counter); 
      int ending = messageFormat.toPattern().indexOf(x); 
      counter = ending + x.length(); 
      tmp.setEnd(ending); 
      points.add(tmp); 
     } 

     for (String x : list) { 
      boolean match = true; 
      for (Points y : points) { 
       if (match) { 
        if (x.substring(y.getStart(), y.getEnd()).equals(messageFormat.toPattern().substring(y.getStart(), y.getEnd()))) { 
        } else { 
         match = false; 
        } 
       } 
      } 
      if (match) { 
       System.out.println("Match!!!"); 
      } else { 
       System.out.println("Not Match!!"); 
      } 
     } 

    } 
} 

이 출력

일치 당신에게 인쇄됩니다 실행!

일치하지 않음 !!

일치하지 않음 !!

일치하지 않음 !!

경기 !!!

일치하지 않음 !!

희망 하시길 바랍니다.