Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if string is a URL with Regex in Java [duplicate]

Possible Duplicates:
What is the best regular expression to check if a string is a valid URL
Java-How to detect the presence of URL in a string.

Is there a way to see if a string is a valid URL? I'm using MIDP library and there are not regular expressions in MIDP. Any help is greatly appreciated.

like image 425
user69514 Avatar asked Dec 05 '22 22:12

user69514


1 Answers

Why not just construct a java.net.URL out of the string and catch any exceptions that might be thrown?

String something;
try {
  URL url = new URL(something);
} catch (MalformedURLException e) {
  // it wasn't a URL
}
like image 128
Edwin Buck Avatar answered Dec 07 '22 12:12

Edwin Buck