2013-07-23 84 views
3

我有這樣的MessageFormat;檢查一個字符串是否與Java中的特定MessageFormat匹配?

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

我只是想知道我是否有一些像這樣的字符串;

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

如何檢查是否使用了MessageFormat中創建的上述字符串?即他們匹配的messageFormat?

非常感謝!

+0

我不明白,你想檢查是一個字符串是類似的消息格式創建?如果這是問題,請將消息傳遞格式解析爲字符串並執行等同。 – 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()) { 
    ... 
} 

正則表達式的說明:

^ = 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 

如果你想捕捉的令牌,使用大括號這樣的:

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

您可以檢索使用mat.group(1)組和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!!"); 
      } 
     } 

    } 
} 

運行,這將打印您輸出

比賽!

不匹配!!

不匹配!!

不匹配!!

比賽!!!

不匹配!!

希望你喜歡它!

相關問題