Last active
August 29, 2015 14:06
-
-
Save vpilot/335a9a5094576a9529b3 to your computer and use it in GitHub Desktop.
This file contains 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 WebLinkValidator { | |
private final static String linkRegex = "((([A-Za-z]{3,9}:(?://)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((?:/[\\+~%/\\.\\w\\-]*)?\\??(?:[\\-\\+=&;%@\\.\\w]*)#?(?:[\\.\\!/\\\\\\w]*))?)"; | |
/** | |
* Examples of valid web links are: | |
* <ul> | |
* <li>"www.site.com"</li> | |
* <li>"http://www.site.com"</li> | |
* <li>"mailto:lucy@site.com"</li> | |
* <li>"lucy@site.com"</li> | |
* <li>"www.site-query.com/?q=a-query-string"</li> | |
* </ul> | |
* | |
* @param webLink string representation of a link to be validated | |
* @return | |
*/ | |
public static boolean isWebLink(String webLink) { | |
try { | |
Pattern patt = Pattern.compile(linkRegex); | |
Matcher matcher = patt.matcher(webLink); | |
return matcher.matches(); | |
} catch (RuntimeException e) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment