URL Regex Java Validator

Search...

⌘K

URL Regex Java Validator

Search...

⌘K


URL Regex Java Validator

URL Regex Java Validator

The URL Regex Java Validator allows developers to validate URLs using regular expressions directly in Java. Whether you’re working with HTTP endpoints, redirect URLs, or validating form input, this tool helps ensure your links are clean, valid, and secure.

You can also explore the Email Regex Java Validator, Phone Number Regex Java Validator, or UUID Regex Java Validator for additional input formats often used alongside URLs.

https://www.admin.google.com/
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "https://www.admin.google.com/" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is URL Regex in Java?

A URL (Uniform Resource Locator) points to a web resource. Validating its structure using regex ensures it starts with the right protocol (e.g., HTTP/HTTPS), includes a domain, and optionally contains paths or query strings.


Common components of a URL include:

  • Protocol (http, https)

  • Domain name (e.g., google.com)

  • Path (e.g., /search)

  • Optional port (e.g., :8080)

  • Query parameters (e.g., ?q=java)


import java.util.regex.*;

public class URLValidator {
    public static boolean isValidURL(String url) {
        String regex = "https?://(?:www\\.)?[a-zA-Z0-9./]+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);
        return matcher.matches();
    }

    public static void main(String[] args) {
        String url = "https://www.example.com";
        System.out.println("Is URL valid? " + isValidURL(url));
    }
}


URL Regex Pattern (Java)

A robust regex for URL validation in Java is:

^(https?://)?([\\w-]+\\.)+[\\w-]{2,}(\\:[0-9]{1,5})?(/[\\w-./?%&=]*)?$


This pattern checks for:

  • Optional http:// or https://

  • Subdomains and top-level domain

  • Optional port

  • Optional path/query


How It Works

  1. Paste your URL into the input box.

  2. Enter or select the appropriate Java regex pattern.

  3. Instantly check if the string matches a valid URL format.


Java Code Example

This checks for proper protocol, domain, and optional path/query parameters.

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class URLValidator {
    public static void main(String[] args) {
        String url = "https://www.qodex.ai/tools/url-validator";
        String regex = "^(https?://)?([\\w-]+\\.)+[\\w-]{2,}(\\:[0-9]{1,5})?(/[\\w-./?%&=]*)?$";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);

        if (matcher.matches()) {
            System.out.println("Valid URL!");
        } else {
            System.out.println("Invalid URL.");
        }
    }
}


Examples

  • http://example.com – Valid

  • https://blog.qodex.ai/tools/validator – Valid

  • htp://example.com – Invalid protocol

  • www..example.com – Double dots are not valid

  • https://example.com:8080/page?query=test – Valid with port and query


Use Cases

  • Web Form Validation: Ensure users enter valid website URLs in input fields.

  • Crawlers/Scrapers: Filter only valid links from large datasets.

  • API Security: Validate callback URLs or redirect URIs.

  • Link Shorteners: Accept and check original URLs for validity.


Pro Tips

  • Always use https? if you want to match both HTTP and HTTPS protocols.

  • Escape characters like . and ? properly in Java with double backslashes (\\.).

  • If matching complex query strings, consider expanding the regex to cover special characters like #, =, or @.

  • Want to validate IP-based URLs? Combine with IP Address Regex Java Validator.

  • Add [\\w-]{2,} for stricter TLD validation (like .com, .org, .tech).

  • Pair this with the Email Regex Java Validator when validating contact forms that require both fields.


Combine with These Tools

Frequently asked questions

Can this regex validate both HTTP and HTTPS?×
Yes, use https?:// to match both protocols.
Will this work for IP-based URLs?+
Can I match subdomains?+
Does it support query parameters?+
Should I validate URLs using regex or libraries?+