Cataclysm
Cataclysm

Reputation: 8538

Regular expression String for URL in JAVA

I would like to check URL Validation in JAVA with regular-expression. I found this comment and I tried to use it in my code as follow...

private static final String PATTERN_URL = "/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?‌​(?:[\w]*))?)/";


.....
if (!urlString.matches(PATTERN_URL)) {
  System.err.println("Invalid URL");
  return false;
}

But I got compile time exception for writing my PATTERN_URL variable. I have no idea how to format it and I am worried about will it become invalid regex if I have modified. Can anyone fix it for me without losing original ? Thanks for your helps.

Upvotes: 1

Views: 131

Answers (1)

aliteralmind
aliteralmind

Reputation: 20163

Your regex looks fine. You just need to format it for a Java string, by escaping all the escape-slashes:

\ --> \\

Resulting in this:

"/((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?‌​(?:[\\w]*))?)/"

After Java interprets this string into a java.util.regex.Pattern, it will strip out those extra escape-slashes and become exactly the regex you want. You can prove this by printing it:

System.out.println(Pattern.compile(PATTERN_URL));

Upvotes: 4

Related Questions