我有一個像下面的代碼中提到的通配符列表(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);
}
}
}
}
請幫我用正則表達式來達到上述目的。 任何幫助將不勝感激。
什麼你的意思是不給予正確的答案做?這是正確的答案?你能否提供投入和預期產出的例子? – Elmer 2013-05-07 14:57:28
正如我所提到的,這裏的輸入是String uri =「cr/eRControl/work/custom.jsp」;它應該從列表中返回匹配的url作爲https:// cr/eRControl/* – 2013-05-07 15:00:33