fork(7) 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. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. //String[] URLs = new String[] { "http://f...content-available-to-author-only...o.com/blah_blah", "http://f...content-available-to-author-only...o.com/blah_blah/", "http://f...content-available-to-author-only...o.com/blah_blah_(wikipedia)", "http://f...content-available-to-author-only...o.bar?q=Spaces should be encoded" };
  13. //String[] URLs = new String[] { "[20:56] <@Котофеич> http://s...content-available-to-author-only...l.com/pics/pics_original/3/4/2/5825243.jpg" };
  14. // The URL above is invalid since it contains consecutive dots
  15. String[] URLs = new String[] { "[20:56] <@Котофеич> http://s...content-available-to-author-only...l.com/pics/pics_original/3/4/2/5825243.jpg" };
  16. Pattern REGEX = Pattern.compile("(?i)\\b(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?\\b");
  17. for (String url : URLs) {
  18. Matcher matcher = REGEX.matcher(url);
  19. if (matcher.find()) {
  20. System.out.println(matcher.group());
  21. }
  22.  
  23. }
  24.  
  25. }
  26. }
Success #stdin #stdout 0.11s 320256KB
stdin
Standard input is empty
stdout
http://s...content-available-to-author-only...l.com/pics/pics_original/3/4/2/5825243.jpg