fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11.  
  12. public static
  13. ArrayList retrieveLinks(String text) {
  14. ArrayList links = new ArrayList();
  15.  
  16. String regex = "\\(?\\b(http://|https://|www[.])?[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
  17. Pattern p = Pattern.compile(regex);
  18. Matcher m = p.matcher(text);
  19. while(m.find()) {
  20. String urlStr = m.group();
  21. char[] stringArray1 = urlStr.toCharArray();
  22.  
  23. if (urlStr.startsWith("(") && urlStr.endsWith(")"))
  24. {
  25.  
  26. char[] stringArray = urlStr.toCharArray();
  27.  
  28. char[] newArray = new char[stringArray.length-2];
  29. System.arraycopy(stringArray, 1, newArray, 0, stringArray.length-2);
  30. urlStr = new String(newArray);
  31. // System.out.println("Finally Url ="+newArray.toString());
  32.  
  33. }
  34. //System.out.println("...Url..."+urlStr);
  35. links.add(urlStr);
  36. }
  37. return links;
  38.  
  39. }
  40.  
  41. public static void main (String[] args) throws java.lang.Exception
  42. {
  43. System.out.println("starting");
  44. ArrayList l = retrieveLinks("site.com test.com test.com/viva/index.htm");
  45. for (Object o : l) {
  46. System.out.println(o);
  47. }
  48. }
  49. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
starting
site.com
test.com
test.com/viva/index.htm