2013-05-02 150 views
0

我試圖匹配特定字符串的模式,這將是靜態的,下面是我的程序:的Java模式匹配特定模式來匹配

package com.test.poc; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class RegexTestPatternMatcher { 
    public static final String EXAMPLE_TEST = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 

    public static void main(String[] args) { 
    Pattern pattern = Pattern.compile("{\\w+}"); 
    // In case you would like to ignore case sensitivity you could use this 
    // statement 
    // Pattern pattern = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE); 
    Matcher matcher = pattern.matcher(EXAMPLE_TEST); 
    // Check all occurance 
    while (matcher.find()) { 
     System.out.print("Start index: " + matcher.start()); 
     System.out.print(" End index: " + matcher.end() + " "); 
     System.out.println(matcher.group()); 
    } 
    // Now create a new pattern and matcher to replace whitespace with tabs 
    Pattern replace = Pattern.compile("\\s+"); 
    Matcher matcher2 = replace.matcher(EXAMPLE_TEST); 
    System.out.println(matcher2.replaceAll("\t")); 
    } 
} 

我嘗試匹配可用的字符串在{}並替換它們有一定的價值。

,但它給了我這樣的例外:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition 
{\w+} 
    at java.util.regex.Pattern.error(Pattern.java:1713) 
    at java.util.regex.Pattern.closure(Pattern.java:2775) 
    at java.util.regex.Pattern.sequence(Pattern.java:1889) 
    at java.util.regex.Pattern.expr(Pattern.java:1752) 
    at java.util.regex.Pattern.compile(Pattern.java:1460) 
    at java.util.regex.Pattern.<init>(Pattern.java:1133) 
    at java.util.regex.Pattern.compile(Pattern.java:823) 
    at com.test.poc.RegexTestPatternMatcher.main(RegexTestPatternMatcher.java:9) 

可能是什麼問題,我在這裏做。對不起,我要問這個位置

回答

5

{}是保留字符。 你需要逃避那些: \\{\\}

作爲參考,這些字符用於重複。

http://www.regular-expressions.info/repeat.html

編輯

我不相信你能做到使用匹配的這種風格簡單的更換。 什麼你可以做的是建立一個新的字符串,先後查找每一場比賽:

public static void main(String[] args) { 
    String toConvert = EXAMPLE_TEST; 

    Pattern pattern = Pattern.compile("\\{\\w+\\}"); 
    // In case you would like to ignore case sensitivity you could use this 
    // statement 
    // Pattern pattern = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE); 
    Matcher matcher = pattern.matcher(toConvert); 
    StringBuilder resultStringBuilder = new StringBuilder(); 
    int startPos = 0; 
    while (matcher.find()) { 

     // append everything up to this match. 
     resultStringBuilder.append(toConvert.substring(startPos, matcher.start())); 

     // append the replacement 
     resultStringBuilder.append(lookup(matcher.group())); 

     // set the start pos for the next match 
     startPos = matcher.end(); 
    } 
    // append everything that's left. 
    resultStringBuilder.append(toConvert.substring(startPos, toConvert.length()));   


    String resultStrig = resultStringBuilder.toString(); 
    System.out.println(resultStrig); 
} 


private static String lookup(String s) { 
    // decide what you want to replace this string with 
    // You might want to make use of a TreeMap<String,String> here. 
    return ""; 
} 
+0

由於它的工作就像魅力+ 1。如何用現在的價值取代它們? – 2013-05-02 12:32:43

+0

我已經編輯了對該問題的答案;-) – 2013-05-03 09:28:28

0

試試這個:

Pattern pattern = Pattern.compile("\\{\\w+\\}");