Skip to content

Instantly share code, notes, and snippets.

@vpilot
Last active August 29, 2015 14:06
Show Gist options
  • Save vpilot/335a9a5094576a9529b3 to your computer and use it in GitHub Desktop.
Save vpilot/335a9a5094576a9529b3 to your computer and use it in GitHub Desktop.
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