fork(31) 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. Pattern REGEX = Pattern.compile("(?i)^(?:(?: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*)?$");
  14. for (String url : URLs) {
  15. Matcher matcher = REGEX.matcher(url);
  16. if (matcher.find()) {
  17. System.out.println(matcher.group());
  18. }
  19.  
  20. }
  21.  
  22. }
  23. }
Success #stdin #stdout 0.11s 320320KB
stdin
Standard input is empty
stdout
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)