从一串字符串中匹配URL地址 正则 (可以没有http或https开头)

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


public class helloWorld2 {
    private static final String TRACK_SEPARATE = ",";
    public static void main(String[] args) {
        String str = "\t<html>\n" +
                "\t\t<body>\n" +
                "\t\t\t<a href=\"http://stackoverflow.com/questions/ask-1-1-1\" /  >\n" +
                "\t\t\t<a href=\"http://stackoverflow.com/questions/ask\" >kkkkkk</a>\n" +
                "\t\t\t<img width=\"584\" height=\"154\" alt=\"BIGLOBE WiMAX 2+\" src=\"http://img.google.com/images/shopbnr/imax2_160517_584_154\">\n" +
                "\t\t</body>\n" +
                "\t</html>";


        getStrings(str);
    }


    private static void getStrings( String str) {
        Pattern p = Pattern.compile("((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)|[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)");
        Matcher m = p.matcher(str);
        ArrayList<String> strs = new ArrayList<String>();
        while (m.find()) {
            strs.add(m.group(1));
        }
        for (String s : strs){
            System.out.println(s);
        }
    }
}



http://stackoverflow.com/questions/ask-1-1-1
http://stackoverflow.com/questions/ask
http://img.gpoint.co.jp/images/shopbnr/biglobewimax2_160517_584_154

Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8

Process finished with exit code 0


Pattern p = Pattern.compile("((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)|[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)");



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值