Skip to content

Instantly share code, notes, and snippets.

@slava-konashkov
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save slava-konashkov/9648910 to your computer and use it in GitHub Desktop.

Select an option

Save slava-konashkov/9648910 to your computer and use it in GitHub Desktop.
Java URL checker RehExp
public class Main {
/**
* - Всеволод! У меня тут в программе 100 ошибок! не проходит валидацию!
* - Попробуй решить их через регулярные выражения.
* - Всеволод, у меня теперь в программе 101 ошибка!
*/
final static String sUrlRegex = "^(https?|ftp|file)://([a-z\\.-_]+/)+(\\??[a-z0-9]+=[a-z0-9]+\\&?)*";
public static void chechUrl(String sUrl) throws Exception {
if (!sUrl.matches(sUrlRegex)) {
throw new Exception("Invalid URL");
}
}
public static void main(String[] args) {
String sUrl = "";
try {
sUrl = args[0];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error input URL");
}
try {
chechUrl(sUrl);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment