Last active
August 29, 2015 13:57
-
-
Save slava-konashkov/9648910 to your computer and use it in GitHub Desktop.
Java URL checker RehExp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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