donchen Posted December 10, 2009 Share Posted December 10, 2009 Hi guys, I am coding in JAVA and in my program, a user is required to enter a URL. I want to use a regular expression to validate if the user's URL entered is valid or not. (eg of not valid: http://ww-.youtube.com). Anyone can assist please? Regards Don Chen Link to comment Share on other sites More sharing options...
0 dontocsata Posted December 10, 2009 Share Posted December 10, 2009 Hi guys,I am coding in JAVA and in my program, a user is required to enter a URL. I want to use a regular expression to validate if the user's URL entered is valid or not. (eg of not valid: http://ww-.youtube.com). Anyone can assist please? Regards Don Chen Here's a hacked together regex: (http|https):\/\/[A-Za-z\.]*\.(com|org|net) You can easily add ftp or whatever to the first list eg (http|https|ftp) And you can add only .whatevers eg (com|org|net|info) or to except any... (http|https):\/\/[A-Za-z\.]*\.[A-Za-z]* (this will match http://www.youtube though) Pattern p = Pattern.compile("(http|https):\\/\\/[A-Za-z\\.]*\\.(com|org|net)"); String url = "http://ww-.youtube.com"; Matcher m = p.matcher(url); System.out.println(m.matches()); Link to comment Share on other sites More sharing options...
0 zeroday Posted December 10, 2009 Share Posted December 10, 2009 This is the one I found in the Android source code: public static final Matcher WEB = Pattern.compile( new StringBuilder() .append("((?:(http|https|Http|Https|rtsp|Rtsp):") .append("\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)") .append("\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_") .append("\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?") .append("((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}\\.)+") // named host .append("(?:") // plus top level domain .append("(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])") .append("|(?:biz|b[abdefghijmnorstvwyz])") .append("|(?:cat|com|coop|c[acdfghiklmnoruvxyz])") .append("|d[ejkmoz]") .append("|(?:edu|e[cegrstu])") .append("|f[ijkmor]") .append("|(?:gov|g[abdefghilmnpqrstuwy])") .append("|h[kmnrtu]") .append("|(?:info|int|i[delmnoqrst])") .append("|(?:jobs|j[emop])") .append("|k[eghimnrwyz]") .append("|l[abcikrstuvy]") .append("|(?:mil|mobi|museum|m[acdghklmnopqrstuvwxyz])") .append("|(?:name|net|n[acefgilopruz])") .append("|(?:org|om)") .append("|(?:pro|p[aefghklmnrstwy])") .append("|qa") .append("|r[eouw]") .append("|s[abcdeghijklmnortuvyz]") .append("|(?:tel|travel|t[cdfghjklmnoprtvwz])") .append("|u[agkmsyz]") .append("|v[aceginu]") .append("|w[fs]") .append("|y[etu]") .append("|z[amw]))") .append("|(?:(?:25[0-5]|2[0-4]") // or ip address .append("[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]") .append("|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]") .append("[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}") .append("|[1-9][0-9]|[0-9])))") .append("(?:\\:\\d{1,5})?)") // plus option port number .append("(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~") // plus option query params .append("\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?") .append("(?:\\b|$)").toString() ).matcher(""); Link to comment Share on other sites More sharing options...
0 donchen Posted December 11, 2009 Author Share Posted December 11, 2009 This is the one I found in the Android source code: public static final Matcher WEB = Pattern.compile( new StringBuilder() .append("((?:(http|https|Http|Https|rtsp|Rtsp):") .append("\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)") .append("\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_") .append("\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?") .append("((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}\\.)+") // named host .append("(?:") // plus top level domain .append("(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])") .append("|(?:biz|b[abdefghijmnorstvwyz])") .append("|(?:cat|com|coop|c[acdfghiklmnoruvxyz])") .append("|d[ejkmoz]") .append("|(?:edu|e[cegrstu])") .append("|f[ijkmor]") .append("|(?:gov|g[abdefghilmnpqrstuwy])") .append("|h[kmnrtu]") .append("|(?:info|int|i[delmnoqrst])") .append("|(?:jobs|j[emop])") .append("|k[eghimnrwyz]") .append("|l[abcikrstuvy]") .append("|(?:mil|mobi|museum|m[acdghklmnopqrstuvwxyz])") .append("|(?:name|net|n[acefgilopruz])") .append("|(?:org|om)") .append("|(?:pro|p[aefghklmnrstwy])") .append("|qa") .append("|r[eouw]") .append("|s[abcdeghijklmnortuvyz]") .append("|(?:tel|travel|t[cdfghjklmnoprtvwz])") .append("|u[agkmsyz]") .append("|v[aceginu]") .append("|w[fs]") .append("|y[etu]") .append("|z[amw]))") .append("|(?:(?:25[0-5]|2[0-4]") // or ip address .append("[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]") .append("|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]") .append("[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}") .append("|[1-9][0-9]|[0-9])))") .append("(?:\\:\\d{1,5})?)") // plus option port number .append("(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~") // plus option query params .append("\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?") .append("(?:\\b|$)").toString() ).matcher(""); WOW! This is one set of serious code... hahaha.. Link to comment Share on other sites More sharing options...
Question
donchen
Hi guys,
I am coding in JAVA and in my program, a user is required to enter a URL.
I want to use a regular expression to validate if the user's URL entered is valid or not. (eg of not valid: http://ww-.youtube.com).
Anyone can assist please?
Regards
Don Chen
Link to comment
Share on other sites
3 answers to this question
Recommended Posts