Skip to content

Instantly share code, notes, and snippets.

@j2deme
Created September 23, 2013 18:30
Show Gist options
  • Save j2deme/6674833 to your computer and use it in GitHub Desktop.
Save j2deme/6674833 to your computer and use it in GitHub Desktop.
Verify if a String is a valid Url

It's very simple to use:

Boolean isValid = isUrl("http://androcode.es");
private static boolean isUrl(String s) {
String regex = "^(https?://)?(([\\w!~*'().&=+$%-]+: )?[\\w!~*'().&=+$%-]+@)?(([0-9]{1,3}\\.){3}[0-9]{1,3}|([\\w!~*'()-]+\\.)*([\\w^-][\\w-]{0,61})?[\\w]\\.[a-z]{2,6})(:[0-9]{1,4})?((/*)|(/+[\\w!~*'().;?:@&=+$,%#-]+)+/*)$";
try {
Pattern patt = Pattern.compile(regex);
Matcher matcher = patt.matcher(s);
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