日期:2014-05-19 浏览次数:21147 次
public static void main(String[] args) { String ss1 = "/admin/review/codeReview.action"; String ss2 = "/review/codeReview.action"; String reg = "(/\\w+)+(\\.action)"; //String reg = "(/\\w+)+(\\.\\w+)"; //不管后缀名采用此 System.out.println(ss1.matches(reg)); System.out.println(ss2.matches(reg)); }
------解决方案--------------------
public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("/admin/123/codeReview.action"); list.add("/admin/review/codeReview.action"); list.add("/review/codeReview.action"); String reg = "(?:/\\w+)*/(\\w+)(?:/\\w+\\.action)"; for (String ss : list) { Pattern p = Pattern.compile(reg); Matcher m = p.matcher(ss); if (m.matches()) { System.out.println(m.group(1)); } } }
------解决方案--------------------
String ss1 = "/admin/review/codeReview.action"; String ss2 = "/review/codeReview.action"; String reg = "/(\\w+)(/\\w+)(\\.action)"; Pattern p = Pattern.compile(reg); Matcher m = p.matcher(ss1); if (m.find()){ System.out.println("group: " + m.group(1)); } m = p.matcher(ss2); if (m.find()){ System.out.println("group: " + m.group(1)); }
------解决方案--------------------
String ss = "/admin/review/codeReview.action";
String[] s = ss.split("/");
System.out.println(s[s.length-2]);
这样好像也可以哦。。。