如何从字符串中提取HTML完整链接?

时间:2015-10-31 05:18:01

标签: java regex matcher

我正在尝试从字符串中提取Html链接。我尝试使用模式和匹配器来查找链接,但我的正则表达式是错误的。我只想获得cloneurlHttp:有任何帮助来解决这个问题吗?

此字符串不在JSon中。

     String s = "{RepositoryMetadata: {AccountId: 329791006272,RepositoryId: 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef,RepositoryName: CodeCommitTest,DefaultBranch: master,LastModifiedDate: Tue Oct 20 13:50:57 ACDT 2015,CreationDate: Tue Oct 13 16:36:03 ACDT 2015,CloneUrlHttp: https://git-codecommit.us-east-1.amazonaws.com/v1/repos/CodeCommitTest,CloneUrlSsh: ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/CodeCommitTest,Arn: arn:aws:codecommit:us-east-1:329791006272:CodeCommitTest}}";

    String pattern = "^\\CloneURLHttp:\\s(.*?)$";
    String httpaddress = "";

    Pattern r = Pattern.compile(pattern);
    Matcher m;



        m = r.matcher(s);
        if (m.find()) {
            httpaddress = m.group(1);

        }


    System.out.println(httpaddress);

1 个答案:

答案 0 :(得分:2)

使用单词边界和否定字符类。

String pattern = "(?i)\\bCloneURLHttp:\\s*([^,]*)";
Pattern r = Pattern.compile(pattern);
Matcher m;
m = r.matcher(s);
String httpaddress = "";
if (m.find()) {
        httpaddress = m.group(1);
 }

DEMO