0

How would I transform a text like the following or any other text containing an URL (http ftp etc)

Go to this link http://www.google.com (ofc stack overflow already does this, on my website this is just plain text);

Into this

Go to this link <a href="http://www.google.com">www.google.com</a>

I've come up with this method

public String transformURLIntoLinks(String text){
 String urlValidationRegex = "(https?|ftp)://(www\\d?|[a-zA-Z0-9]+)?.[a-zA-Z0-9-]+(\\:|.)([a-zA-Z0-9.]+|(\\d+)?)([/?:].*)?";
 Pattern p = Pattern.compile(urlValidationRegex);
 Matcher m = p.matcher(text);
 StringBuffer sb = new StringBuffer();
 while(m.find()){
 String found =m.group(1); //this String is only made of the http or ftp (just the first part of the link)
 m.appendReplacement(sb, "<a href='"+found+"'>"+found+"</a>"); // the result would be <a href="http">"http"</a>
 }
 m.appendTail(sb);
 return sb.toString();
}

The problem is the regexes i've tried match only the first part("http" or "ftp").

My output becomes: Go to this link <a href='http'>http</a>

It should be this

Go to this link <a href='http://www.google.com'>http://www.google.com</a>

asked Jul 16, 2013 at 9:12
4
  • 3
    Unfortunately, SO is not a coding service. If you've tried something yourself that did not work, come back and ask for help. But don't ask us to solve your work. Commented Jul 16, 2013 at 9:13
  • Simple: public String transformURLIntoLinks(String text){ return "Go to this link <a href=\"http://www.google.com\">www.google.com</a>"; } Commented Jul 16, 2013 at 9:27
  • It must transform any text containing an URL to the same text containing the anchor of the url. Commented Jul 16, 2013 at 9:45
  • I've reworded the question and provided an attempted solution. Commented Jul 16, 2013 at 11:09

1 Answer 1

2
public String transformURLIntoLinks(String text){
String urlValidationRegex = "(https?|ftp)://(www\\d?|[a-zA-Z0-9]+)?.[a-zA-Z0-9-]+(\\:|.)([a-zA-Z0-9.]+|(\\d+)?)([/?:].*)?";
Pattern p = Pattern.compile(urlValidationRegex);
Matcher m = p.matcher(text);
StringBuffer sb = new StringBuffer();
while(m.find()){
 String found =m.group(0); 
 m.appendReplacement(sb, "<a href='"+found+"'>"+found+"</a>"); 
}
m.appendTail(sb);
return sb.toString();
 }

m.group(1) was the mistake. m.group(0) works. This will transform any URL found in the text into an anchor.

answered Jul 17, 2013 at 16:09
Sign up to request clarification or add additional context in comments.

1 Comment

This solution doesn`t work for cases where there are multiple links in the string.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.