如何通过正则表达式获取到:jdbc:hsqldb:hsql://localhost:8888/test 里的localhost和8888值

YuLimin 2008-05-24 08:40:27
如何通过正则表达式获取到:jdbc:hsqldb:hsql://localhost:8888/test 里的localhost和8888值
...全文
212 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
全粘架构师 2008-05-24
  • 打赏
  • 举报
回复
不知道你啥意思,正则是判断是不是的,而不是查询的。

如果查询

String url="jdbc:hsqldb:hsql://localhost:8888/test"

int p1=url.indexOf("localhost");
int p2=url.indexOf("8888");


如果你是认为localhost和8888的是变量的话,则用前后截断

int p1=url.indexOf("://");
String s1=url.substring(p1);

int p2=s1.indexOf(":");
String host=s1.substring(0,p2-1);
String s2=s1.substring(p2+1);
String port=s2.substring(0,s2.indexOf("/"));


老紫竹 2008-05-24
  • 打赏
  • 举报
回复
  public static void main(String[] args){
String str = "jdbc:hsqldb:hsql://localhost:8888/test";
Pattern p = Pattern.compile("//(.*?):(\\d*)/");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧