Detect and extract url from a string?

This is a easy question,but I just don’t get it. I want to detect url in a string and replace them with a shorten one.

I found this expression from stackoverflow,But the result is just http

Pattern p = Pattern.compile("\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]",Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(str);
        boolean result = m.find();
        while (result) {
            for (int i = 1; i <= m.groupCount(); i++) {
                str = str.replace(url, shorten(url));
            result = m.find();
        return html;

Is there any better idea?

Answer gives you the first matching group, that is to say the first capturing parenthesis. Here it’s (https?|ftp|file)

You should try to see if there is something in, or surround all your pattern with parenthesis and use again.

You need to repeat your find function to match the next one and use the new group array.

Leave a Reply

Your email address will not be published. Required fields are marked *