0

I am trying to extract an Html link from a string. I tried using patterns and matcher to find the link but my regex is wrong. I only want to get the cloneurlHttp : Any help to fix this?

And this string is not in 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 Answer 1

2

Use word boundaries and negated charcter class.

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.