2013-05-07 337 views
0

我有一個像下面的代碼中提到的通配符列表(lMapValues) 我需要將uri與此列表進行匹配以找到匹配的url。正則表達式匹配url列表中的特定uri

在下面的代碼中,我應該在地圖m中將匹配的url作爲d的值。 這意味着如果uri的一部分在url列表中匹配,則應該選取該特定url。

我試着拆分令牌中的uri,然後檢查列表lMapValues中的每個令牌。但它沒有給我正確的結果。下面是代碼。

public class Matcher 
{ 
    public static void main(String[] args) 
    { 
     Map m = new HashMap(); 
     m.put("a","https:/abc/eRControl/*"); 
     m.put("b","https://abc/xyz/*"); 
     m.put("c","https://work/Mypage/*"); 
     m.put("d","https://cr/eRControl/*"); 
     m.put("e","https://custom/MyApp/*"); 

     List lMapValues = new ArrayList(m.values()); 
     List tokens = new ArrayList(); 

     String uri = "cr/eRControl/work/custom.jsp"; 

     StringTokenizer st = new StringTokenizer(uri,"/"); 
     while(st.hasMoreTokens()) { 

      String token = st.nextToken(); 
      tokens.add(token); 
     } 

     for(int i=0;i<lMapValues.size();i++) { 
      String value = (String)lMapValues.get(i); 
      String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b"; 
      Pattern pattern = Pattern.compile(patternString); 
      java.util.regex.Matcher matcher = pattern.matcher(value); 

      while (matcher.find()) { 
       System.out.println(matcher.group(1)); 
       System.out.println(value); 
      } 
     } 
    } 
} 

請幫我用正則表達式來達到上述目的。 任何幫助將不勝感激。

+0

什麼你的意思是不給予正確的答案做?這是正確的答案?你能否提供投入和預期產出的例子? – Elmer 2013-05-07 14:57:28

+0

正如我所提到的,這裏的輸入是String uri =「cr/eRControl/work/custom.jsp」;它應該從列表中返回匹配的url作爲https:// cr/eRControl/* – 2013-05-07 15:00:33

回答

0

的問題是,i充當關鍵不是作爲一個指數

String value = (String)lMapValues.get(i); 

,您將得到更好的服務的列表交換地圖,並使用每個循環。

List<String> patterns = new ArrayList<String>(); 
... 
for (String pattern : patterns) { 
    .... 
} 
0

這是更簡單的檢查,如果一個字符串與String.indexOf()一定值開始。

String[] urls = { 
    "abc/eRControl", 
    "abc/xyz", 
    "work/Mypage", 
    "cr/eRControl", 
    "custom/MyApp" 
}; 

String uri = "cr/eRControl/work/custom.jsp"; 

for (String url : urls) { 
    if (uri.indexOf(url) == 0) { 
     System.out.println("Matched: " + url); 
    }else{ 
     System.out.println("Not matched: " + url); 
    } 
} 

另外。如果你永遠不會與之匹配,那麼就不需要將該方案存儲到地圖中。

0

如果我正確理解你的目標,你甚至可能在這裏甚至不需要正則表達式。

嘗試......

package test; 

import java.util.HashSet; 
import java.util.Set; 

public class PartialURLMapper { 

    private static final Set<String> PARTIAL_URLS = new HashSet<String>(); 
    static { 
     PARTIAL_URLS.add("cr/eRControl"); 
     // TODO add more partial Strings to check against input 
    } 
    public static String getPartialStringIfMatching(final String input) { 
     if (input != null && !input.isEmpty()) { 
      for (String partial: PARTIAL_URLS) { 
       // this will be case-sensitive 
       if (input.contains(partial)) { 
        return partial; 
       } 
      } 
     } 
     // no partial match found, we return an empty String 
     return ""; 
    } 
    // main method just to add example 
    public static void main(String[] args) { 
     System.out.println(PartialURLMapper.getPartialStringIfMatching("cr/eRControl/work/custom.jsp")); 
    } 

} 

...這將返回:

cr/eRControl