正则表达式初探

最近项目里做关于UC浏览器,QQ浏览器分享到我们应用的功能,返回的数据中包含这样一串string:
这里写图片描述
现在想把URL截出来,就需要用到正则表达式,从浏览器里分享出来的字符串都有一个规范就是在URL的前后会与其他文本留一个空格,So我的思路是先截出来http开头到字符串末尾的文本,然后利用正则匹配字符串截取第一个URL。
我的代码

   public static  String getFirstURLFromTextContent(String textContent){
      String result = null;
        List<String> ls=new ArrayList<String>();
        if (textContent.indexOf("http")!= -1){
            String subTextContent = textContent.substring(textContent.indexOf("http"));
            String formatString = "((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+";
            Pattern pattern = Pattern.compile(formatString);
            Matcher matcher = pattern.matcher(subTextContent);
            while(matcher.find()){
                ls.add(matcher.group());
            }
              result = ls.get(0);
        }
        return  result;
    }

接下来重点看正则表达式,发现这个网站非常的好用,尤其是对于正则表达式一无所知的小白很是实用:正则测试网站
正则表达式测试
网站内容很实用,基本一看就会,我们只需要确认正则是正确的,就可以生成相应的代码在我们工程用使用了。(天下武功,唯快不破)关于正则更多的功能,需要自己研究和学习,这里到此为止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯哥666

谢谢老板鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值