扫码关注官方订阅号
比如有很多地址
www.abc.com/a/xx/bwww.abc.com/a/yy/bwww.abc.com/a/zz/b
其中的xx yy zz可能是任何字符,如果获取某个URI中 www.abc.com/a/xx/b, 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
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
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
输出:
输出:
xx