java 截取URL中的一部分

liuxin52525 2016-12-02 05:50:51
比如 http://www.abc.def.com/123456
或者 https://www.abc.def.com.cn/123456
或者 http://abc.def.com/123456
https://abc.def.com/123456等等
我只想要 abc.def.com 或者 abc.def.com.cn 这块
...全文
2035 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
DAVE-BLACK 2019-06-11
  • 打赏
  • 举报
回复
这个截取不方便。建议你去js里面处理。js中document中包含了请求的信息。根据你的需求可以返回不同的结果。地址或者是后面的参数都能单独返回
zouhuu 2016-12-15
  • 打赏
  • 举报
回复
大神的正则玩的好溜啊
rickylin86 2016-12-15
  • 打赏
  • 举报
回复

import java.util.regex.Matcher;
import java.util.regex.Pattern;
   
public class Test{
    public static void main(String[] args){
        String url = "http://www.abc.def.com:8080/123";
        String url1 = "http://192.168.1.100/admin";
        System.out.println(getDomain(url));
        System.out.println(getDomain(url1));
    }
   
    private static String getDomain(String url){
        String regex = "^.*?://(?:www[.])?(\\w+([.]\\w+)*(:\\d+)?)/.*$"; 
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }
}
liuxin52525 2016-12-13
  • 打赏
  • 举报
回复
引用 9 楼 rickylin86 的回复:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Test{
    public static void main(String[] args){
        String url = "http://www.abc.def.com/123";
        String url1 = "http://192.168.1.100/admin";
        System.out.println(getDomain(url));
        System.out.println(getDomain(url1));
    }
  
    private static String getDomain(String url){
        String regex = "^.*?://(?:www[.])?(\\w+([.]\\w+)*)/.*$"; 
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }
}
大神 突然又有个问题了 我得到的url中有的带端口号 例如 http://abc.def.com.cn:9090/123456 这样 用上边的正则匹配不出内容啊 这样的url 得到abc.def.com.cn 或者 abc.def.com.cn:9090也可以   麻烦大神赐教
rickylin86 2016-12-05
  • 打赏
  • 举报
回复

import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Test{
    public static void main(String[] args){
        String url = "http://www.abc.def.com/123";
        String url1 = "http://192.168.1.100/admin";
        System.out.println(getDomain(url));
        System.out.println(getDomain(url1));
    }
  
    private static String getDomain(String url){
        String regex = "^.*?://(?:www[.])?(\\w+([.]\\w+)*)/.*$"; 
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }
}
liuxin52525 2016-12-05
  • 打赏
  • 举报
回复
引用 7 楼 liuxin52525 的回复:
[quote=引用 5 楼 rickylin86 的回复:]

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test{
    public static void main(String[] args){
        String url = "https://www.abc.def.com.cn/123456 ";
		String url1 = "http://192.168.1.100/admin";
        System.out.println(getDomain(url));
		System.out.println(getDomain(url1));
    }
 
    private static String getDomain(String url){
        String regex = "^.*?://(?:[a-zA-Z]+[.])?(\\w+([.]\\w+)*)/.*$"; 
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }
}
突然发现了问题 http://abc.def.com/123 其实我想要的结果是 abc.def.com 但是上述代码吧abc去掉了 值得到了 def.com 求赐教[/quote] http://www.abc.def.com/123 这种情况 得去掉www. 只要abc.def.com
liuxin52525 2016-12-05
  • 打赏
  • 举报
回复
引用 5 楼 rickylin86 的回复:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test{
    public static void main(String[] args){
        String url = "https://www.abc.def.com.cn/123456 ";
		String url1 = "http://192.168.1.100/admin";
        System.out.println(getDomain(url));
		System.out.println(getDomain(url1));
    }
 
    private static String getDomain(String url){
        String regex = "^.*?://(?:[a-zA-Z]+[.])?(\\w+([.]\\w+)*)/.*$"; 
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }
}
突然发现了问题 http://abc.def.com/123 其实我想要的结果是 abc.def.com 但是上述代码吧abc去掉了 值得到了 def.com 求赐教
liuxin52525 2016-12-03
  • 打赏
  • 举报
回复
引用 5 楼 rickylin86 的回复:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test{
    public static void main(String[] args){
        String url = "https://www.abc.def.com.cn/123456 ";
		String url1 = "http://192.168.1.100/admin";
        System.out.println(getDomain(url));
		System.out.println(getDomain(url1));
    }
 
    private static String getDomain(String url){
        String regex = "^.*?://(?:[a-zA-Z]+[.])?(\\w+([.]\\w+)*)/.*$"; 
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }
}
谢大神赐教
rickylin86 2016-12-03
  • 打赏
  • 举报
回复

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test{
    public static void main(String[] args){
        String url = "https://www.abc.def.com.cn/123456 ";
		String url1 = "http://192.168.1.100/admin";
        System.out.println(getDomain(url));
		System.out.println(getDomain(url1));
    }
 
    private static String getDomain(String url){
        String regex = "^.*?://(?:[a-zA-Z]+[.])?(\\w+([.]\\w+)*)/.*$"; 
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }
}
liuxin52525 2016-12-03
  • 打赏
  • 举报
回复
引用 2 楼 rickylin86 的回复:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{
	public static void main(String[] args){
		String url = "https://www.abc.def.com.cn/123456 ";
		System.out.println(getDomain(url));
	}

	private static String getDomain(String url){
		String regex = "^.*?://\\w+[.](?<domain>\\w+([.]\\w+)*)/.*$";
		Matcher matcher = Pattern.compile(regex).matcher(url);
		String result = null;
		if(matcher.find()){
			result = matcher.group("domain");
		}
		return result;
	}
}
大神 这个特别棒 但是今天我又发现了问题 1是有ip的形式 例如 http://192.168.122.24/pub 2是我在安卓里发现api和java的不一样了 android里面没有matcher.group(String arg) 只有一个matcher.group(int arg) 麻烦大神再给出一个解决办法 小弟谢过
Inhibitory 2016-12-02
  • 打赏
  • 举报
回复
import java.net.URI;
import java.net.URL;

public class Test {
    public static void main(String[] args) throws Exception {
        URL url = new URI("http://www.abc.def.com/123456").toURL();
        System.out.println(url.getHost());
    }
}
rickylin86 2016-12-02
  • 打赏
  • 举报
回复

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{
	public static void main(String[] args){
		String url = "https://www.abc.def.com.cn/123456 ";
		System.out.println(getDomain(url));
	}

	private static String getDomain(String url){
		String regex = "^.*?://\\w+[.](?<domain>\\w+([.]\\w+)*)/.*$";
		Matcher matcher = Pattern.compile(regex).matcher(url);
		String result = null;
		if(matcher.find()){
			result = matcher.group("domain");
		}
		return result;
	}
}
SSHorSSM 2016-12-02
  • 打赏
  • 举报
回复
substring()或者slice() 看行不行吧

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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