Validate URL Format
This program validates URL format using regex.
Problem Statement
Write a Java program to validate URL format.
Source Code
| 1 | public class RegexURLValidation { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Regex URL Validation"); |
| 4 | String url="https://www.eduinq.com"; |
| 5 | String regex="^(https?|ftp)://[^\\s/$.?#].[^\\s]*$"; |
| 6 | if(url.matches(regex)) System.out.println("Valid URL"); |
| 7 | else System.out.println("Invalid URL"); |
| 8 | } |
| 9 | } |
Program Output
Eduinq Regex URL Validation Valid URL
Explanation
The regex checks for a valid protocol prefix (http, https, or ftp) followed by a properly formatted domain and path, and matches() validates the given URL against this pattern.