首页 > Java中如何获取字符串中的关键字?

Java中如何获取字符串中的关键字?

比如有很多地址

www.abc.com/a/xx/b
www.abc.com/a/yy/b
www.abc.com/a/zz/b

其中的xx yy zz可能是任何字符,
如果获取某个URI中 www.abc.com/a/xx/b, xx部分的字符?


    public static void main(String[] args) {
        String str = "www.abc.com/a/xx/b";
        String prefix = "www.abc.com/a/";
        String sufferfix = "/b";
        System.out.println(str.substring(prefix.length(), str.indexOf(sufferfix)));
    }

输出:
xx


public static void main(String[] args) throws Exception{
    String s = "www.abc.com/a/xx/b";
    System.out.println(s.matches("www\\.abc\\.com/a/(\\w+)/b"));
    Pattern er = Pattern.compile("www\\.abc\\.com/a/(\\w+)/b");
    Matcher m = er.matcher(s);
    m.find();
    System.out.println(m.group(1));
}

输出:

true
xx
【热门文章】
【热门文章】